# SQL query through script

**URL:** <https://forum.inductiveautomation.com/t/sql-query-through-script/44210>\
**Category:** Ignition\
**Created:** [March 8, 2021, 7:24am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210 "2021-03-08T07:24:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kp.krishnaprasad97](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@kp.krishnaprasad97](https://forum.inductiveautomation.com/u/kp.krishnaprasad97)\
**Post date:** [March 8, 2021, 7:24am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/1 "2021-03-08T07:24:57Z")

</div>

How to run SQL query in python script using built in function in ignition and syntax  
query=“SELECT ID,EQUIPMENT,STREAMFUNCTION,Message FROM Messages  
WHERE STREAMFUNCTION LIKE “%S6%” and Reply = 1  
ORDER BY ID DESC  
LIMIT 3”

---

<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:** [March 8, 2021, 10:17am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/2 "2021-03-08T10:17:24Z")

</div>

Probably can’t, because you have used double quotes in your LIKE clause–that isn’t valid. SQL requires single quotes for values. (Make sure queries run in the designer’s query browser to find such problems.)

After you fix that, try the DB scripting functions described in the manual, starting with [system.db.runQuery()](https://docs.inductiveautomation.com/display/DOC81/system.db.runQuery).

---

<div class="post-metadata">

**Author:** ![kp.krishnaprasad97](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@kp.krishnaprasad97](https://forum.inductiveautomation.com/u/kp.krishnaprasad97)\
**Post date:** [March 8, 2021, 10:31am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/3 "2021-03-08T10:31:51Z")

</div>

Okay,  
I will do that

---

<div class="post-metadata">

**Author:** ![nelson.hurtado](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nelson.hurtado/32/18950_2.png) [@nelson.hurtado](https://forum.inductiveautomation.com/u/nelson.hurtado)\
**Post date:** [December 1, 2022, 4:02am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/4 "2022-12-01T04:02:48Z")

</div>

> [@pturmel](#):
>
> Probably can't, because you have used double quotes in your LIKE clause--that isn't valid. SQL requires single quotes for values. (Make sure queries run in the designer's query browser to find such problems.)
> 
> After you fix that, try the DB scripting functions described in the manual, starting with [system.db.runQuery()](https://docs.inductiveautomation.com/display/DOC81/system.db.runQuery).

I tried to run the next query, succesfully:  
table = system.db.runQuery("SELECT DISTINCT name FROM users INNER JOIN user\_ci ON users.id = user\_ci.user\_id WHERE date IS NULL AND station\_number = 18", "MySQL\_DB")

but now I want to do a dynamic query including a STRING VAR (station) instead of 18:  
station = 18  
table = system.db.runQuery("SELECT DISTINCT name FROM users INNER JOIN user\_ci ON users.id = user\_ci.user\_id WHERE date IS NULL AND station\_number = 'station' ", "MySQL\_DB")  
it runs without any result! How can I do that (station\_number is INT and station is STRING).

---

<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:** [December 1, 2022, 7:45am UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/5 "2022-12-01T07:45:19Z")

</div>

[https://docs.inductiveautomation.com/display/DOC81/Named+Queries](https://docs.inductiveautomation.com/display/DOC81/Named+Queries)

[https://docs.inductiveautomation.com/display/DOC81/Named+Query+Parameters](https://docs.inductiveautomation.com/display/DOC81/Named+Query+Parameters)

Pay attention to the security risks when using QueryStrings.

* * *

Tip: use the \</\> code formatting button to preserve code indentation and apply syntax highlighting. There's an edit button 🖉 below your post so you can fix it.

---

<div class="post-metadata">

**Author:** ![ShaneH](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@ShaneH](https://forum.inductiveautomation.com/u/ShaneH)\
**Post date:** [December 1, 2022, 4:47pm UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/6 "2022-12-01T16:47:42Z")

</div>

You might try using runPrepQuery which lets you pass arguments to your query:

> [@](#):
>
> station = 18  
> query = "SELECT DISTINCT name FROM users INNER JOIN user\_ci ON users.id = user\_ci.user\_id WHERE date IS NULL AND station\_number = ?"  
> vals = [station]  
> db = "MySQL\_DB"  
> table = system.db.runPrepQuery(query, vals, db)

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

HTH.  
-Shane

---

<div class="post-metadata">

**Author:** ![nelson.hurtado](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nelson.hurtado/32/18950_2.png) [@nelson.hurtado](https://forum.inductiveautomation.com/u/nelson.hurtado)\
**Post date:** [December 1, 2022, 6:03pm UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/7 "2022-12-01T18:03:24Z")

</div>

I really appreciate your quick reply Mr. Shane:  
For all people I found at night another way with the same successful result:

table = system.db.runQuery("SELECT DISTINCT name FROM users INNER JOIN user\_ci ON users.id = user\_ci.user\_id WHERE date IS NULL AND station\_number = %s"%(station), "MySQL\_DB")

I hope it helps a lot of people.

---

<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:** [December 1, 2022, 6:05pm UTC](https://forum.inductiveautomation.com/t/sql-query-through-script/44210/8 "2022-12-01T18:05:54Z")

</div>

Depending on the source of the `station` variable, you're opening yourself to SQL injection risks. Use a prepared statement. In addition to being safer, it's more performant.
