Scripting problem in vision

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

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

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.


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:

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

	foo = 8
	print bar
	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:

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

	foo = 8
	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.


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.
	select_query = "SELECT id FROM users WHERE username = ?"

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

	db_connection = "Database"

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

1 Like

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