Writing Variable Rows to DB

I have a process that requires me to be able to write between 1 to 15 new values to a database depending on what is happening. I was able to do this in a client, but I lost something when I tried to make it a gateway script and I can’t find the error in the wrapper.log to read what is happening. This is the script I was trying to use:

scanned=system.tag.getTagValue(“Packaging/10A/BagsScanned”)
values=[system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_0_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_1_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_2_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_3_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_4_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_5_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_6_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_7_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_8_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_9_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_10_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_11_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_12_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_13_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_14_”),system.tag.getTagValue(“Packaging/10A/Scale_10_a_Weight_array_15_”)]
time=newValue.timestamp
for j in range(scanned):
weight=values[0][j]
system.db.runPrepUpdate(“INSERT INTO Giveaway_10A (Bag_Weight, time_stamp) VALUES (?,?)”,[weight,time])

The formatting isn’t cooperating for me but the indentation in the for loop should be correct. I looked into doing this with block groups too but the rows I want to add aren’t guaranteed to change they could be the same value each time. Any ideas or questions?

Hi David,

The values variable is a list of tag values. But the loop is only getting the first item in the list (values[0]).

Perhaps replace values[0][j] with values[j].

It didn’t fix the problem, but it is possible there is more than one issue too. Thanks for the suggestion.

DavidKettle, try specifying the database connection a the third parameter:

for j in range(scanned): system.db.runPrepUpdate("INSERT INTO Giveaway_10A (Bag_Weight, time_stamp) VALUES (?,?)",[values[j],time], "yourDbConnectionName")

I specified the database and kept the change nmudge suggested as well and the script is working great so far.

Thanks both of you for your help!

Yayyy! Good team work :smiley: