# Scripting problem in vision

**URL:** <https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381>\
**Category:** Ignition\
**Tags:** ignition81, vision, scripting\
**Created:** [August 25, 2023, 10:45am UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381 "2023-08-25T10:45:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya\_Chandra\_Mohan](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@Surya\_Chandra\_Mohan](https://forum.inductiveautomation.com/u/Surya_Chandra_Mohan)\
**Post date:** [August 25, 2023, 10:45am UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/1 "2023-08-25T10:45:38Z")

</div>

Hello,  
I am trying to get the user\_id of a particular user when username is selected drom the dropdown in vision but user\_id is being inserted as 0 in the table  
Can you please help me out with this issue

 ![2023-08-25 (1)](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/1/01e4771ac2dd1344348f729ba0e966eb0f6542f5.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 25, 2023, 11:41am UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/2 "2023-08-25T11:41:37Z")

</div>

You have everything you are trying to do in that script in the outermost level, not guarded by an

```auto
if event.propertyName=='selectedValue':

```

condition check. Vision propertyChange events run for **every change** to **every property** on that component, which is certainly screwing up your logic.

Indent your entire script one more level. Put the `if` statement on the first line.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 25, 2023, 12:05pm UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/3 "2023-08-25T12:05:40Z")

</div>

A few other things about this script:

- If you're only expecting one value out of the query, or if you only want the first result, you could make it a scalar query instead
- You don't need to check for both `result` and `len(result) > 0`, they'll both return the same thing
- I understand this is a test script that's not made for production, but I'd advice against use raw `except`, particularly if you're handling only one error (here, you're printing that the insert failed. What if something else failed ? You'll lose your mind trying to debug this.)

edit: Here's an example of that last point:

```python
def never_42(foo):
	if foo == 42:
		raise ValueError("foo == 42 !")

try:
	foo = 8
	never_42(foo)
	print bar
except:
	print "foo == 42 !"

```

This will print "foo == 42 !" which will lead you to believe that, indeed, `foo == 42`.  
But if we change one little thing:

```python
def never_42(foo):
	if foo == 42:
		raise ValueError("foo == 42 !")

try:
	foo = 8
	never_42(foo)
	print bar
except Exception as e:
	print e

```

The except clause still catches everything (well... almost, it won't catch Java errors), but now it prints `name 'bar' is not defined`, which makes finding the actual error much easier.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 25, 2023, 12:54pm UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/4 "2023-08-25T12:54:23Z")

</div>

To add:

- It looks like you're mixing parameters for different query functions and strings. Consider using prepared statements and _system.db.runPrepQuery()_ and _system.db.runPrepUpdate()_
- The db connection should already be configured in the gateway. IMO _system.db.getConnectionInfo()_ is not useful in this instance.

```python
	select_query = "SELECT id FROM users WHERE username = ?"

	insert_query = "INSERT into demo (user_id) VALUES (?)"

	db_connection = "Database"

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 25, 2023, 12:57pm UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/5 "2023-08-25T12:57:15Z")

</div>

As should be mentioned, please post the actual script instead of a screenshot of the script to make it easier to review.

> [@Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831):
>
> This thread is intended as a short help file for all those struggling to post formatted code on the forum. Posting formatted code on the forum (and most others) is as simple as selecting the code and pressing the \</\> button. It works the same as applying Bold or Italic styles to your document. It preserves indentation and applies syntax highlighting. [How to post code]

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 25, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/scripting-problem-in-vision/78381/6 "2023-08-25T13:05:56Z")

</div>

> [@JordanCClark](#):
>
> It looks like you're mixing parameters for different query functions and strings. Consider using prepared statements and _system.db.runPrepQuery()_ and _system.db.runPrepUpdate()_

ooooh, didn't even notice that !  
Most likely the cause of the issue.
