# system.db.runUpdateQuery stopped working?

**URL:** <https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665>\
**Category:** Ignition\
**Created:** [October 23, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665 "2024-10-23T13:41:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Graham](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Joshua\_Graham](https://forum.inductiveautomation.com/u/Joshua_Graham)\
**Post date:** [October 23, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665/1 "2024-10-23T13:41:27Z")

</div>

I've had a script for a few tags writing to an SQL database, yesterday they randomly stopped working and I'm not sure what the cause is.

I find this error in the diagnostics.

> Error executing script.  
> Traceback (most recent call last):  
> File "tagevent:valueChanged", line 10, in valueChanged  
> at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:392)  
> at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runUpdateQuery(AbstractDBUtilities.java:188)  
> at jdk.internal.reflect.GeneratedMethodAccessor516.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.runUpdateQuery(INSERT INTO DataCollectionCounts VALUES ('UCPRESS', 'GOODPART', '2024-10-23 09:38:59.814'), Live\_CEPDataEntry, , false)

This is the code in the script,

```python
	from java.text import SimpleDateFormat
	qv = currentValue.value
	outputFormat=SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
	Current_datetime = system.date.now()
	Current_datetime_STR=outputFormat.format(Current_datetime) 

	if qv:
		system.db.runUpdateQuery("INSERT INTO DataCollectionCounts VALUES ('UCPRESS', 'GOODPART', '%s')" % (Current_datetime_STR),"Live_CEPDataEntry")

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 23, 2024, 1:44pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665/2 "2024-10-23T13:44:56Z")

</div>

Show the full error stack trace. There will be a "Caused By:" part that will tell us exactly what went wrong.

Also highly recommend using the `prep` version of update query. Then you do not have to do any string formatting of dates, you can pass along `system.date.now()` directly as an arg to your query. Or use Named Queries and same thing. No string formatting necessary. See here - [system.db.runPrepUpdate | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 23, 2024, 1:54pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665/3 "2024-10-23T13:54:41Z")

</div>

And if you _do_ need to format the date (which should really only be done at the point of display for human consumption) use the Ignition built-in function, [system.date.format | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-date/system-date-format), which avoids loading an external library.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 23, 2024, 2:08pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665/4 "2024-10-23T14:08:31Z")

</div>

> [@bkarabinchak.psi](#):
>
> Also highly recommend using the `prep` version of update query

In addition to this, you really shouldn't touch a DB in value change events.

This script should be in a Project Script Library, and called from a Gateway Tag Change event.

Script should really be just this:

```python
if currentValue.value and not initialChange:
    query = "INSERT INTO DataCollectionCounts VALUES('UCPRESS','GOODPART',?)"
    system.db.runPrepUpdate(query,[system.date.now()],"Live_CEPDataEntry")

```

Though in a Gateway Tag Change Event, you would replace `currentValue` with `newValue`

Note my use of the initialChange flag, this will block the query from running when the tag is initialized, you could be getting false records without it.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 23, 2024, 2:13pm UTC](https://forum.inductiveautomation.com/t/system-db-runupdatequery-stopped-working/97665/5 "2024-10-23T14:13:00Z")

</div>

True, every time the value changes it has to `from java.text import SimpleDateFormat` again whereas in a project script library that would be cached. However it shouldn't be used anyways as @Transistor pointed out.

Given the other two values inserted are hardcoded strings, my money is on the date being the issue here.
