Ok, I’ve got a script written which grabs some time-stamped XML data and returns a dictionary where the keys represent the column name. How would I go about getting this dictionary inserted into my sql table as a row?
I could run a transaction group where each tag an expression item like
x = myfunction()
x[key]
but I don’t want to run this script and perform a new XML fetch for every tag. Would rather fetch the XML once and parse it into this dictionary, then access the dictionary to build my insert queries. I’m sure there’s a way to do this, I’m just not coming up with it…
[quote=“jsham”]Ok, I’ve got a script written which grabs some time-stamped XML data and returns a dictionary where the keys represent the column name. How would I go about getting this dictionary inserted into my sql table as a row?
I could run a transaction group where each tag an expression item like
x = myfunction()
x[key]
but I don’t want to run this script and perform a new XML fetch for every tag. Would rather fetch the XML once and parse it into this dictionary, then access the dictionary to build my insert queries. I’m sure there’s a way to do this, I’m just not coming up with it…[/quote]
your saying you have a table with two columns and you want to store the key and value in this table?
Pretty sure all the parenthesis are in order. the concatenation is because the runScript function expects a string and you can’t insert a reference directly into the string. Ah well.
Thanks! So I could bind that runscript expression to a SQLTag and just change the scan class to determine the interval that I insert. Is there a good way to run something like this in a transaction group?
I get lost on all the scope rules with Ignition scripting, but can I run my parsing script as a gateway timer script and store the resulting dictionary in a scope where I can then access the dictionary object from SQLTags? Then I can directly use the value, or bind it to a table or label, or I can write it to the DB using a transaction group, which would allow all of the good things that come with tx groups (triggering, etc).
You can actually run this as an expression item within a transaction group.
The only thing you may want to consider is combining our two functions into a single one. I’m just not sure how an expression item would handle a dictionary. They’re not quite the same thing as a list.
def toQuery(table, dict):
dictKeys=dict.keys()
dictValues=dict.values()
query="INSERT INTO "+str(table)
queryCols="("
queryVals="VALUES("
for x in range(len(dictKeys)):
queryCols+=str(dictKeys[x])+","
queryVals+="'"+str(dictValues[x])+"',"
return query+queryCols[:-1]+") "+queryVals[:-1]+")"
def toDataSet(dict):
import system
dictKeys=dict.keys()
dictValues=dict.values()
rows=[]
row=[]
for y in range(len(dictKeys)):
row.append(dictValues[y])
rows.append(row)
print dictKeys
print rows
return system.dataset.toDataSet(dictKeys,rows)
To the IA guys, I have no idea yet on how to assign a dataset within a transaction group like this, though… will it return as a dataset object?
EDIT: whoops! Meaning if I use the dataset function above…