# Gateway Script not inserting records to table

**URL:** <https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [August 16, 2023, 9:29pm UTC](https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961 "2023-08-16T21:29:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wendy\_Minai](https://avatars.discourse-cdn.com/v4/letter/w/48db29/32.png) [@Wendy\_Minai](https://forum.inductiveautomation.com/u/Wendy_Minai)\
**Post date:** [August 16, 2023, 9:29pm UTC](https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961/1 "2023-08-16T21:29:53Z")

</div>

I have this code as my gateway script but it is not inserting records to my desired table. Please someone help me figure out what's wrong with it.

```auto
if system.tag.readBlocking("[default]zzzTimezzz/demotest")[0].value == True:
	                               
	excluded_twisters = []
	
	for i in range(1, 63): # Iterate over all Twisters (1 to 62)
	   exclude_tag_path = ["[default]Twisters/TW%s/TW_Data_Shift/TW_Data_Prev_Shift_%s" % (i, shift) for i in range(1, 63)]
	   exclude_tag_value = system.tag.readBlocking(exclude_tag_path)[0].value == True
	   
	   if not exclude_tag_value: # Check if the exclude tag is not true
	       excluded_twisters.append(i) # Add Twister number to the exclusion list
	
	# Define the line of Twisters you want to process (excluding those in excluded_twisters)
	line = [twister for twister in range(1, 63) if twister not in excluded_twisters]
	
	# Rest of your script remains unchanged
	for i in line:
	   tagPathLeft_Eff = "[default]Twisters/TW" + str(i) + "/Left/Shift_1/Previous_Efficiency"
	   tagPathRight_Eff = "[default]Twisters/TW" + str(i) + "/Right/Shift_1/Previous_Efficiency"
	   Left_Eff = system.tag.readBlocking(tagPathLeft_Eff)[0].value
	   Right_Eff = system.tag.readBlocking(tagPathRight_Eff)[0].value
	   Twisters = i
	   DateTime = system.date.now()
	   Shift = system.tag.readBlocking("[default]Twisters/Previous Shift")[0].value[0]
	
	   Supervisor_mapping = {
	       ('J', True): 'Carol Brookeshire' if Twisters <= 32 else 'Lind Smith',
	       ('K', True): 'Andrew Frazier' if Twisters <= 32 else 'Tracy Presley',
	       ('L', True): 'Sandra McIntyre' if Twisters <= 33 else 'Tim Foley',
	       ('M', True): 'Juanita Gordon' if Twisters <= 33 else 'Jan Kelly',
	   }
	   
	   Supervisor = Supervisor_mapping.get((Shift, True), 'Unknown Supervisor')
	   
	   system.db.runPrepUpdate("INSERT INTO Plt65_DayShift_Twisting_Efficiencies (Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff) VALUES(?,?,?,?,?,?)",

```

```
                          [Twisters, DateTime, Shift, Supervisor, Left_Eff, Right_Eff], 'IgnitionHUD')

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 17, 2023, 12:51pm UTC](https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961/3 "2023-08-17T12:51:37Z")

</div>

What error are you getting?

Running SQL commands in a gateway context you should be providing the database connection you want to use I suspect this is the issue.

It's always easiest to start with the error message it will tell you exactly what is going wrong. Go to your gateway logs trigger this script and see what pops up.

---

<div class="post-metadata">

**Author:** ![mwolter](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mwolter](https://forum.inductiveautomation.com/u/mwolter)\
**Post date:** [August 17, 2023, 4:49pm UTC](https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961/4 "2023-08-17T16:49:51Z")

</div>

I had this issue, and most of the interaction in our project is controlled by SQL.  
For instance, this line of code:  
system.db.runPrepUpdate("INSERT INTO [OMSSCADA]. [dbo].[workorder\_StartStop] ([workorder\_Type],[Batch\_Number],[PRRNumber],[Batch\_SS],[Batch\_Product],[NAV\_Product],[Operator],[Start\_tstamp],[tstamp])VALUES(?,?,?,?,?,?,?,?,?)",[workorderType,BatchNumber,PRRNumber,Batch\_SS,Batch\_Product,NAV\_Product,Operator,Start\_tstamp,tstamp])  
If I tried to run it without the Database and SCHEMA designator ([OMSSCADA]. [dbo].) before the table, the SQL would fail.  
Just a thought...

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 17, 2023, 5:05pm UTC](https://forum.inductiveautomation.com/t/gateway-script-not-inserting-records-to-table/77961/5 "2023-08-17T17:05:40Z")

</div>

> [@bkarabinchak.psi](#):
>
> Running SQL commands in a gateway context you should be providing the database connection you want to use I suspect this is the issue.

Only if they don't have a default database configured for the project, since Gateway scripts are project resources they can use the projects defaults. If no default is configured then, definitely an issue.

@Wendy_Minai What is the need for the tuple as key in the `Supervisor_mapping`? Its not an issue, I'm just curious as to the thought process as, you're not using it (at least in what you've shown)?
