# Python Sql Insert Code

**URL:** <https://forum.inductiveautomation.com/t/python-sql-insert-code/56160>\
**Category:** Ignition\
**Tags:** python, database, scripting\
**Created:** [February 15, 2022, 11:28am UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160 "2022-02-15T11:28:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [February 15, 2022, 11:28am UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/1 "2022-02-15T11:28:23Z")

</div>

Hello all,

I have a problem about “insert into” in SQL… You can see the output of error and my script code in below.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/9/89cf590464b2ca467e304d9e4f7008155378ba76.png)

**Code:**

```auto
if retVal ['success']:
	datas = retVal['data']
	system.db.runUpdateQuery("INSERT INTO AutoDispatchTRA VALUES('%(lineabbreviation)s', '%(areacode)s', '%(linecode)s', '%(tradecode)s', '%(dispatchtypecode)s', '%(started)s', '%(closed)s', '%(machinecode)s', '%(reason)s', '%(dispatchnumber)i')" %datas, database_name)

```

I request API call and I get “success”… After that, the script continue with “IF” condition as above. But then, I get this error.

Actually, I can see the my “INSERT” information in the end of diagnostic lines and each values are correct. But I couldn’t understand what’s going on.

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [February 15, 2022, 11:41am UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/2 "2022-02-15T11:41:06Z")

</div>

> [@eren.uyanik](#):
>
> `'%(areacode)s'`

you really should use runPrepQuery or runPrepUpdate instead of trying to convert the value to a string by putting it in between ''

[https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepUpdate](https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepUpdate)

---

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [February 15, 2022, 11:59am UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/3 "2022-02-15T11:59:21Z")

</div>

Thanks for your answer.  
But even if I try with “runPrepUpdate”, I get similar error.

```auto
if retVal ['success']:
       datas = retVal['data']
       system.db.runPrepUpdate("INSERT INTO AutoDispatchTRA (LineAbbreviation, AreaCode, LineCode, TradeCode, DispatchTypeCode, StartedTime, ClosedTime, MachineCode, Reason, DispatchNumber) VALUES (?,?,?,?,?,?,?,?,?)", [datas['lineabbreviation'], datas['areacode'], datas['linecode'], datas['tradecode'], datas['dispatchtypecode'], datas['started'], datas['closed'], datas['machinecode'], datas['reason'], datas['dispatchnumber']], database_name)

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/b/ab98ccd86df60f9c9e9c738f8173efcf334b138b.png)

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [February 15, 2022, 12:29pm UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/4 "2022-02-15T12:29:19Z")

</div>

Then im guessing its somethign wrong with the query?

try putting this sql query directly in your db manager, they might give you a more detailed error meessage

---

<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:** [February 15, 2022, 12:33pm UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/5 "2022-02-15T12:33:15Z")

</div>

You have 10 columns inserted to, you have 9 question marks

Also when SQL reports back the values it appears some are null, there are 2 instances with 2 commas in a row

---

<div class="post-metadata">

**Author:** ![eren.uyanik](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eren.uyanik/32/30923_2.png) [@eren.uyanik](https://forum.inductiveautomation.com/u/eren.uyanik)\
**Post date:** [February 15, 2022, 12:36pm UTC](https://forum.inductiveautomation.com/t/python-sql-insert-code/56160/6 "2022-02-15T12:36:50Z")

</div>

Yes you are right. I noticed that too before I checked here. Thank you
