Script Console & query output

Hi all,

Again, new to Ignition, dba, and scripting. I am testing scripts in the script console.

When I run a query, i get a dataset rather than the actual value of the cell.

Dataset [1R ⅹ 1C]

This is the result of this script…

user_id = system.db.runNamedQuery(“Test Query”)

print user_id

So two questions:

Can I make this assign the value of the dataset to user_id?

Is the output of the Script Console the same as the output of a component event script? i.e., if I ran this in the event script would it also assign a dataset to user_id?

it is just easier to get the script working correctly in the Script Console.

Thanks for the help,

Steven

Yes. The manual documents the return values of the various database scripting functions.

You can extract values from the dataset like this (the two zeros are for row 0 and column 0):

user_id = system.db.runNamedQuery("Test Query").getValueAt(0, 0)

Datasets in Ignition 8 Manual

Sometimes, it's useful to print the contents of a dataset while developing.

1 Like

If you are only expecting one item to be returned, you can run a Scalar query instead

1 Like

@Matrix_Engineering

That makes that easy!!! That is exactly what I needed.

@witman, @pturmel. & @JordanCClark, thanks for the ideas and examples. I will certainly put it to good use.

Again, thank you all for the help!!

Steven

1 Like

So as a followup, I used a scalar query to get a single index of a row…

user_id = system.db.runNamedQuery(“username_select_index”, {“name” :name})

This query returns the index number of the row or “None” if the name is not present.

I use an if statement to add the name to the table if not there…

if user_id == None:
system.db.runNamedQuery(“username_update”, {“name”:name})

This works as expected and life is good.

However, when learning how to put this together, I tried to capture and display the value of user_id.
When user_id is a number I can display it. However, when it is “None”, I have not been able to display it.

I get errors depending on what I am trying to display it in. Seems to be a datatype issue. So what datatype is “None”? It works as written in the if statement.

This is not a consequential issue as it works in the script. I would just like to understand it for future reference.

Thanks, Steven

None is a keyword used to represent a Null value, Object, or No value at all.

None has the NoneType datatype, and actually is the only "object" that can have that datatype. You can not create other NoneType objects.

None is not the same as False, 0, or an empty string. When comparing objects to None it will return False for anything other than None.

Python, and Jython by extension, are not Strongly Typed languages (meaning you, the programmer, declare the type of the object). Instead they work on a concept known as duck typing.

Think of it like this "If it looks like a duck and it acts like a duck, it is a duck." In a similar way "if it looks like an integer and it acts like and integer it is an integer".

So user_id assumes the type of the return value of system.db.runNamedQuery(). If that value is an integer then user_id is an integer, if the value is NoneType then user_id is NoneType.

This is why your conditional statement works, because it will return true, if and only if User_Id is None.

So, why all the confusion about what None is? Well, because, there is a little hiccup in the way None works.

A class is free to implement comparison any way it chooses, and it can choose to make comparison against None mean something.

Reference here.

Same is true for functions. So when you print(None) the function "decides" that what you meant was actually something more like print(""). And if a function is expecting an Integer it may choose to treat None as zero.

The most pythonic way to write this is actually to use the "is not" comparison.

userID = system.db.runNamedQuery(“username_select_index”, {“name” :name})

if userID Is Not None:
    system.db.runNamedQuery(“username_update”, {“name”:name})
3 Likes

@lrose,

Thank you so much for the explanation. This explains what I was seeing. I will surely refer to this explanation as I learn and do more.

Thanks!!

Steven