Gateway Script not inserting records to table

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.

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')

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.

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

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)?

1 Like