INSERT String Tags to db records

INSERT String Tags to db records
Aloha Mates!

I'm struggling with being able to INSERT db records from tag event script. You all are my ChatGPT backup.

We have a Fairbanks FB2560 Truck Scale.
This device is connected to Ignition over Ethernet via standard TCP Driver.

The FB scale has always printed hardcopy tickets. These hardcopy tickets are manually entered into Airtable.
(I'd like to see us use our MSSQL db and get rid of Airtable subscription.)

The INBOUND half of the ticket is printed when the harvest truck arrives.
About 10 minutes later, the OUTBOUND half of the ticket is printed when the truck has unloaded and is ready to leave.

Tickets contain various data pertaining to Crew #, LoopID, Truck #, Ticket #, Date-Timestamp, etc.
I've configured the scale to send the same data as ticket info, out its COM4 port.

I've created the following string tags:
"[default]Scale1_" receives FB scale string (anywhere from 115 chars to 155 chars). This tag has a Value_Changed event script.
"[default]Scale1_MemIN" has a Value_Changed event script that parses INBOUND data to individual INBOUND related tags.
"[default]Scale1_MemOUT" has a Value_Changed event script that parses OUTBOUND data to individual OUTBOUND related tags.
"[default]Scale1_trig_Crew" crew# is parsed out of "[default]Scale1_MemIN" and written here, as a string
"[default]Scale1_trig_LoopID" loopid# is parsed out of "[default]Scale1_MemIN" and written here, as a string

The "[default]Scale1_" script simply sends INBOUND data to [default]Scale1_MemIN, and OUTBOUND data to [default]Scale1_MemOUT. This works.
"[default]Scale1_MemIN" and "[default]Scale1_MemOUT" parse to individual tags. This works.
I'm only presenting (2) of the (15) tags that parse to.

PROBLEM:
I want to write the individually parsed tags to fields in our MSSQL db.
(Enabling History doesn't work because some fields don't change value for some time, so there's no corresponding db entry for that piece at a given timestamp.)
(Clearing field first works but then I have extra blank entries in db.)

I don't have SQL Bridge module.
I've been unsuccessful with correct syntax to force a db INSERT at the end of MemIN/MemOUT scripts.

Value_Changed script in "[default]Scale1_MemIN"

	#Parse INBOUND data from Fairbanks 2560 scale 
	import re
	sourceValue = system.tag.read("[default]Scale1_MemIN").value

	#When data does arrive, make sure there is enough before parsing.
	#Occasionally, we get a timeout between Ignition and Fairbanks Scale device.
	#This results in only partial population of the INBOUND data, less than 5 characters.
	if len(sourceValue) > 100:
	
		str = 'CREW NO.\s+(\d+)'
		result = re.findall(str,sourceValue)
		crew_value = result
		system.tag.writeBlocking("[default]Scale1_trig_Crew", result)
	
		str = 'LOOP ID\s+(\d+)'
		result = re.findall(str,sourceValue)
		loop_id_value = result
		system.tag.writeBlocking("[default]Scale1_trig_LoopID", result)
	
	# Get the current epoch timestamp (since t_stamp uses epoch time)
	epoch_time = system.date.toMillis(system.date.now())
	
	# Define the SQL query to insert data into sqlt_data_1_2024_11 for Crew
	query_crew = """
	INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp)
	VALUES (?, ?, ?)
	"""
	# Execute the query to insert Crew (tagid 692)
    system.db.runPrepUpdate(query_crew, [692, crew_value, epoch_time], "SCADA")

	## Define the SQL query to insert data into sqlt_data_1_2024_11 for LoopID
	query_loop_id = """
	INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp)
	VALUES (?, ?, ?)
	"""

	# Execute the query to insert LoopID (tagid 693)
	system.db.runPrepUpdate(query_loop_id, [693, loop_id_value, epoch_time], "SCADA")

What is the fault/error you get from this?

I get crickets. Designer console is blank. Gateway console, I'm pruning garbage out so I can see something meaningful.

In script console I get this, plus another page and a half.

Java Traceback:
Traceback (most recent call last):
  File "<input>", line 34, in <module>
INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp)
VALUES (?, ?, ?)
, [692, [u'1'], 1732163499506], MSSQL, , false, false)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:392)

	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:268)

	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(
INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp)
VALUES (?, ?, ?)
, [692, [u'1'], 1732163499506], MSSQL, , false, false)

Oh, line 34 is the...
system.db.runPrepUpdate(query_crew, [692, crew_value, epoch_time], "MSSQL")

Are you sure you are referencing the right DB here? Your code says "SCADA" your error says MSSQL?

You're good! Sorry, I've tried both names. Pardon my ignorance, if I go to my SSMS, it's called SCADA. That name is nowhere in the Ignition structure, so I tried both.

What is the DB connection called in the Ignition Gateway? That DB reference is not an SQL database name, its the database driver connection in Ignition itself, so it should be the one in your gateway.

MSSQL
I get the same error in script console with MSSQL.

So, in your gateway section like the picture here: https://www.docs.inductiveautomation.com/assets/images/database-connections-status-038d3dbc8cb5d6b2e203d49b29a3cbf3.png

Your "Name" for the connection is "MSSQL"?

Yes.

So also, if you open the Database Browser in Designer, can you run this query:

INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp)
VALUES (692, [u'1'], GETDATE())

Does that work in there?

Error running query: INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp) VALUES (692, [u'1'], GETDATE()) SQL error for "INSERT INTO sqlt_data_1_2024_11 (tagid, stringvalue, t_stamp) VALUES (692, [u'1'], GETDATE())": Invalid column name 'u'1''.

Oh, so the 'U' is the problem?

You should be logging to a dedicated table for this purpose, not overloading the historian table. This is not going to maintain well.

1 Like

The u'1' equates to a unicode "1", but im not sure how runPrepUpdate deals with unicode strings.

Ah, I see what you mean. Hmmm, ok. Back to the drawing board.

Another thing to figure out...how to create a table, what the structure should be, how am I going to use the data in that table.........oy.

Thanks to you both!

The brackets are the issue here:
[u’1’]
MSSQL uses brackets to signal column names, as the error suggests.
Get rid of these and the query should work, though as Paul said I really wouldn’t insert things manually in historian tables. There’s a function for that: system.tag.storeTagHistory.
But even then, if what you’re storing isn’t historical data, you should make a table specifically designed for your use case.

1 Like