Using tag value in SQL query

I am running an event based SQl query to time stamp an estop

I need help using the tag name in my script. my python is useless

My script works as follows

system.db.runUpdateQuery(“INSERT INTO LTN4_ESTOP (t_stamp, estop_name, message) VALUE (‘t_stamp’, ‘tagName’, ‘EStop Active’)”)

I want to use the value of a tag called t_stamp and the name of my tag and insert it in to my query so that it updates the table with the name of the estop that has been pushed and at what time

I want to update the UDT so this is a generic script for all ESTOP Tags

Thank you in advance

I’ve sorted out the timestamp issue I just need to be able to pass the tag name value in to my SQL query


take a look a the “system.db.runPrepUpdate command”

its probably what I would use. Take a look and come back if you need help.

don’t forget to use “” or “system.tag.readAll” in your script to get the tag name value

Ah… :blush:

I’ve just realised that you actually want the tag name, not value

tagPath = 'Your Tags Path' tagName = system.tag.getAttribute(tagPath,"Name")

system.tag.getAttribute has been depreciated but it still works, I’m not sure of the latest method

You could do this in the Tag Value Changed event script

[code]estopMessage = ‘EStop Inactive’
if currentValue == 1:
estopMessage = ‘EStop Active’

system.db.runPrepUpdate(“INSERT INTO LTN4_ESTOP (t_stamp, estop_name, message) VALUE (NOW(), ?, ?)”, [tagPath, estopMessage], yourDatabaseConnection)[/code]

thanks for the help, i will test today and let you know how i get on

i keep getting an error in my code

i rolled back to a simple update query so that i got to a known good situation

i created a TEST instance of the UDT to frig the value to 1 to test if it wrote to the db, each time I change the value to one I get the little red dot error after the tag and I cant see what the error is on the console, it doesnt write to the db either, the system.tag.write line is proven and has been working for a while

see attached for code

Try adding the database connection name to the runUpdateQuery statement. Since tag event scripts are executed on the Gateway there is no default database connection to use.