Problem with inserting a datetime to SQL server from Ignition button

Even better is running a prepUpdate: https://docs.inductiveautomation.com/display/DOC79/system.db.runPrepUpdate

The prepUpdate will take care of the quoting, and send the parameter to the JDBC driver as a date/time object. Then the JDBC driver is responsible for making sure the DB understands the date/time.

What you do now is converting the date into a string to a format your SQL DB understands. But that’s DB specific.

system.db.runPrepUpdate("INSERT INTO acd_history(t_stamp, success) VALUES (?,1)", [event.source.parent.startTime])

Letting the JDBC driver take care of the quoting also makes it safer against (accidental or malicious) SQL injection. Though for complete protection against SQL injection, you need to use Named Queries with the parameters.

3 Likes