Updating database table via tag change script

I have a single boolean tag which represents whether or not my filter is currently in backwash. Whenever this value changes I wish to record an appropriate message in my backwash log database table.

I’m trying to execute the following script whenever my tag changes value.

system.db.runUpdateQuery("INSERT INTO backwash_log(event) VALUES ('Filter x backwash started')")

This script appears to be correct. I’ve tested it in the script console, in client scripts, in Gateway scripts, in component scripting. All of them work correctly. Yet, if I paste this into my tag’s ‘Value Changed’ event script it fails to execute and the gear icon displays a small red x beside it. I’ve tried some of the other functions; runQuery, runPrepUpdate, runUpdateQuery. All seem to behave the same way.

I could probably work around the problem, but this is by far the most straightforward option and it should be possible. Does anyone know what I’m doing wrong here?

You probably failed to specify which database connection to use. Tag events are not in project scope, so there’s no default connection. You must include the database connection name in the function call.

Phil, you’re a machine! Problem solved. Thanks very much.

Hello conlonj25,
I know this is old topic but can you explain how exactly you solved problem?

As stated by @pturmel, the gateway does not have a default database connection, so you must provide it with one.

EDIT: If you get into the habit of always stating the database connection, there is no confusion later on what connection is being used.

query = 'UPDATE alarmstatus SET unacknowledged = 0'
dbConn = 'myDBConnectionName'
system.db.runUpdateQuery(query, dbConn)
3 Likes

Thank you @JordanCClark very, very much.

I ran into the same issue and this thread helped out. Thank you !
Must include database connection if it is using default database.