# system.db.runPrepQuery working but throwing an error

**URL:** <https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [October 9, 2019, 2:29pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960 "2019-10-09T14:29:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GreggCavazos](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@GreggCavazos](https://forum.inductiveautomation.com/u/GreggCavazos)\
**Post date:** [October 9, 2019, 2:29pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960/1 "2019-10-09T14:29:29Z")

</div>

I am having an issue with a script, I havent been able to figure it out, it works but throws an error.

for testing I am using a memory tag with the script on change.

First I attempted to have the query in a string variable called query, but it was not working, after multiple changes, this scripts writes to the DB, but throws the error in the Tag Diagnostics shown below

I have the following script,

```auto
try:
		t_stamp = system.date.now()
		line = 'TestLN'
		station = 'VirSta'
		request_SN = '1234567890'
		request_ModelNo = 'ModelNumber'
		request_Rev = 'WW'
		requestType = 'RequestType'
		request_Timestamp = system.date.now()
		
		system.db.runPrepQuery("INSERT INTO dbo.AFN_PHSRequests ( t_stamp, line, station, request_SN, request_ModelNo, request_Rev, requestType, request_Timestamp)" \
					" VALUES (?,?,?,?,?,?,?,? ) " ,[t_stamp , line, station, request_SN, request_ModelNo, request_Rev, requestType , request_Timestamp], "IGN_DB")
		
	except Exception, ex:
		messagetoPrint = "Error inserting data into table, query: " + str(query) + " Exception:"+ str(ex.message)
		shared.GW_Scripts.LogErrorMessage("AFN Requests",messagetoPrint)

```

I get the following error.

```auto
Error executing script.
Traceback (most recent call last):
  File "<tagevent:valueChanged>", line 18, in valueChanged
  File "<tagevent:valueChanged>", line 18, in valueChanged
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)
	at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepQuery(AbstractDBUtilities.java:287)
	at sun.reflect.GeneratedMethodAccessor113.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepQuery(INSERT INTO dbo.AFN_PHSRequests ( t_stamp, line, station, request_SN, request_ModelNo, request_Rev, requestType, request_Timestamp) VALUES (?,?,?,?,?,?,?,? ) , IGN_DB, [Wed Oct 09 09:22:54 CDT 2019, TestLN, VirSta, 1234567890, ModelNumber, WW, RequestType, Wed Oct 09 09:22:54 CDT 2019], )

```

I have cleared the table a few times and thats why there are only a few resutls of my tests.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/8/8dcfcb729281575d0c8b5332dfd01d851a38f8bd.png)

I guess it should be something simple but I cannot figure it out.

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [October 9, 2019, 2:37pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960/2 "2019-10-09T14:37:58Z")

</div>

> [@GreggCavazos](#):
>
> system.db.runPrepQuery("INSERT INTO dbo.AFN\_PHSRequests ( t\_stamp, line, station, request\_SN, request\_ModelNo, request\_Rev, requestType, request\_Timestamp)" \ " VALUES (?,?,?,?,?,?,?,? ) "

why is VALUES (?,....) in double quotes?

Try:

system.db.runPrepQuery("INSERT INTO dbo.AFN\_PHSRequests ( t\_stamp, line, station, request\_SN, request\_ModelNo, request\_Rev, requestType, request\_Timestamp) VALUES (?,?,?,?,?,?,?,? ) "

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [October 9, 2019, 2:42pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960/3 "2019-10-09T14:42:14Z")

</div>

If your intention was to split that line into multiple lines, you need to use triple quotes “”" start and end

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [October 9, 2019, 2:48pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960/4 "2019-10-09T14:48:41Z")

</div>

Wait, that system function should only be used for SELECT queries. Use **system.db. runPrepUpdate** for INSERT statements

---

<div class="post-metadata">

**Author:** ![GreggCavazos](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@GreggCavazos](https://forum.inductiveautomation.com/u/GreggCavazos)\
**Post date:** [October 9, 2019, 2:54pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-working-but-throwing-an-error/28960/5 "2019-10-09T14:54:31Z")

</div>

> [@Matrix\_Engineering](#):
>
> Use **system.db. runPrepUpdate** for INSERT statements

Thanks that was the issue!!
