# Writing Variable Rows to DB

**URL:** <https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444>\
**Category:** Ignition\
**Created:** [May 14, 2014, 7:19pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444 "2014-05-14T19:19:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidKettle](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@DavidKettle](https://forum.inductiveautomation.com/u/DavidKettle)\
**Post date:** [May 14, 2014, 7:19pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/1 "2014-05-14T19:19:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [May 14, 2014, 7:45pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/2 "2014-05-14T19:45:49Z")

</div>

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].

---

<div class="post-metadata">

**Author:** ![DavidKettle](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@DavidKettle](https://forum.inductiveautomation.com/u/DavidKettle)\
**Post date:** [May 14, 2014, 8:46pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/3 "2014-05-14T20:46:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anna](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anna](https://forum.inductiveautomation.com/u/anna)\
**Post date:** [May 15, 2014, 2:54pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/4 "2014-05-15T14:54:28Z")

</div>

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")`

---

<div class="post-metadata">

**Author:** ![DavidKettle](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@DavidKettle](https://forum.inductiveautomation.com/u/DavidKettle)\
**Post date:** [May 15, 2014, 4:41pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/5 "2014-05-15T16:41:03Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [May 15, 2014, 5:24pm UTC](https://forum.inductiveautomation.com/t/writing-variable-rows-to-db/8444/6 "2014-05-15T17:24:47Z")

</div>

Yayyy! Good team work 😃
