# Problem with system.db.RunQuery in a Tag Event Script

**URL:** <https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078>\
**Category:** Ignition\
**Created:** [January 10, 2024, 9:21pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078 "2024-01-10T21:21:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuel.croft-trembla](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@samuel.croft-trembla](https://forum.inductiveautomation.com/u/samuel.croft-trembla)\
**Post date:** [January 10, 2024, 9:21pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/1 "2024-01-10T21:21:37Z")

</div>

Hello,

I'm trying to extract a UUID from my database into tag when a value change on a tag. I tried running it in a tag event Script but also in a Gateway tag change and I was not able to make it work  
Here is the Script:

```
selector_1 = system.tag.readBlocking(["[LaserWelding]parametres/source_1_selector"])[0].value
source_1_partid = system.tag.readBlocking(["[LaserWelding]parametres/source_1_partid"])[0].value
source_1_matid = system.tag.readBlocking(["[LaserWelding]parametres/source_1_matid"])[0].value
projet = 'A1-022961-01'

if selector_1 == 0:
	system.tag.writeBlocking(["[LaserWelding]parametres/source_1_uuid"],'')
if selector_1 == 1:
	cast = system.db.runQuery("Select part_uuid from casting.experiment where project_id = '%s' and experiment_id = %s" %(projet,source_1_partid))
	system.tag.writeBlocking(["[LaserWelding]parametres/source_1_uuid"],cast)

```

Here is the Error in the Log:

([LaserWelding]parametres/source\_1\_partid, valueChanged) Error executing tag event script: Traceback (most recent call last): File "tagevent:valueChanged", line 12, in valueChanged at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:333) 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.runQuery(Select part\_uuid from casting.experiment where project\_id = 'A1-022961-01' and experiment\_id = 400, , )

Thanks for your feedback!

---

<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:** [January 10, 2024, 9:27pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/2 "2024-01-10T21:27:21Z")

</div>

Which is line 12?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 10, 2024, 9:29pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/3 "2024-01-10T21:29:27Z")

</div>

There's more details in the logs somewhere that would help show the actual problem. Copy every piece of information you can, from the gateway logging interface or the wrapper.log file directly.

---

<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:** [January 10, 2024, 9:38pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/4 "2024-01-10T21:38:53Z")

</div>

One thing to check: When run in the gateway you need to tell it which database connection to use as it is not aware of the project's default.

**system.db.runQuery(query, database, tx)**

See the first syntax variant in [system.db.runQuery - Ignition User Manual 8.0 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC80/system.db.runQuery).

---

<div class="post-metadata">

**Author:** ![samuel.croft-trembla](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@samuel.croft-trembla](https://forum.inductiveautomation.com/u/samuel.croft-trembla)\
**Post date:** [January 10, 2024, 10:04pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/5 "2024-01-10T22:04:05Z")

</div>

Thanks for all the reply. I went but with a query tag with an event driven instead of a fixed rate. Our goal is to hunt all the query against the DB and try to limit them to only have query when needed.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [January 11, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/6 "2024-01-11T14:02:44Z")

</div>

> [@samuel.croft-trembla](#):
>
> `(projet,source_1_partid))`

Is `projet` a typo? Perhaps it is meant to be `project`?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 11, 2024, 2:18pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/7 "2024-01-11T14:18:54Z")

</div>

> [@justinedwards.jle](#):
>
> Is `projet` a typo?

It's french. There's other clues in the OP.

---

<div class="post-metadata">

**Author:** ![samuel.croft-trembla](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@samuel.croft-trembla](https://forum.inductiveautomation.com/u/samuel.croft-trembla)\
**Post date:** [January 11, 2024, 2:20pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/8 "2024-01-11T14:20:07Z")

</div>

Yeah sorry we are a french main language organization.

---

<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:** [January 11, 2024, 3:15pm UTC](https://forum.inductiveautomation.com/t/problem-with-system-db-runquery-in-a-tag-event-script/84078/9 "2024-01-11T15:15:30Z")

</div>

Fairly certain that @Transistor is on the correct track here, since you're calling this in a Gateway Scope you need to provide a database connection name. I don't trust the default to always work anyway.

Also, don't use multiple calls to read blocking, combine them into one.

I would strongly recommend using a Prep Query, because you could also be having issues with data type conversions.

Try this:

```auto
selector_1, source_1_partid, source_1_matid = [qv.value for qv in system.tag.readBlocking(["[LaserWelding]parametres/source_1_selector","[LaserWelding]parametres/source_1_partid","[LaserWelding]parametres/source_1_matid"])]
projet = 'A1-022961-01'
databaseName = 'your Database Connection Name'
cast = ''
if selector_1:
	cast = system.db.runPrepQuery("Select part_uuid from casting.experiment where project_id = ? and experiment_id = ?",[projet,source_1_partid],databaseName)

system.tag.writeBlocking(["[LaserWelding]parametres/source_1_uuid"],cast)

```
