# system.db.runQuery line continuation

**URL:** <https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103>\
**Category:** Ignition\
**Created:** [October 11, 2018, 8:25pm UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103 "2018-10-11T20:25:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [October 11, 2018, 8:25pm UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103/1 "2018-10-11T20:25:37Z")

</div>

Hi guys,

I am trying to run a python executed SQL query when a dropdown list’s focus is lost (after a selection is made).

The query string is very large and I would like to code it in multiple lines.

Is there a way to do this or is 1 long string the only way?

---

<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:** [October 11, 2018, 8:34pm UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103/2 "2018-10-11T20:34:01Z")

</div>

Python supports wrapping strings in a bunch of different ways, but what I use most often for things like queries is a triple-comment:

```auto
query = """anything in this
block of text
is part of the same
literal string"""

```

---

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [October 11, 2018, 10:40pm UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103/3 "2018-10-11T22:40:18Z")

</div>

Hi again, I am having trouble with using placeholders with your method...

> QueryString = ("""  
> SELECT OrderNo as [Order #],  
> Priority,  
> DueDate as [Due Date],  
> PartNo as [Part #],  
> PartDesc as [Part Description],  
> SUBSTRING(b.MaterialName,1, CHARINDEX('-',b.MaterialName) - 1) as [Membrane Part #],  
> REPLACE(m.ClassCode, 'MEMB-','') as [Membrane Material],  
> QueuedQty as [Quantity]  
> FROM vwLots l LEFT OUTER JOIN vwBOM b ON l.PartNo = b.BOMMaterialName  
> AND b.ItemType = 'INPUT'  
> AND b.MatlClassCode = 'SLITROLL'  
> AND b.MaterialName NOT LIKE 'MT%'  
> LEFT OUTER JOIN Material m ON m.MaterialName = SUBSTRING(b.MaterialName,1, CHARINDEX('-',b.MaterialName) - 1)  
> WHERE RouteStepName = 'VA'  
> AND LotID NOT LIKE 'A%'  
> AND LotID NOT LIKE 'OT%'  
> AND OrderNo = %s  
> ORDER BY Priority ASC, DueDate ASC""" % event.source.selectedLabel)  
> QueryTable = system.db.runQuery(QueryString, "MES")  
> event.source.parent.getComponent('TP\_MPN').text = QueryTable[0][5]

Can you explain what I am doing wrong?

---

<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:** [October 11, 2018, 10:52pm UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103/4 "2018-10-11T22:52:01Z")

</div>

Python uses `%s` (and a few other identifiers) as the keyword for string replacement - so at lines in your string like `AND LotID NOT LIKE 'A%'` it thinks you want to replace `%'` using the `'` format specifier - which is invalid. Replace the _literal_ percentages in your output string with _double_ percentages in the Python code to get the correct output:

```python
QueryString = ("""
SELECT OrderNo as [Order #],
FROM vwLots l LEFT OUTER JOIN vwBOM b ON l.PartNo = b.BOMMaterialName 
AND b.MaterialName NOT LIKE 'MT%%'
AND OrderNo = %s""" % 1234)

```

Outputs:

```sql
SELECT OrderNo as [Order #],
FROM vwLots l LEFT OUTER JOIN vwBOM b ON l.PartNo = b.BOMMaterialName 
AND b.MaterialName NOT LIKE 'MT%'
AND OrderNo = 1234

```

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [October 12, 2018, 10:55am UTC](https://forum.inductiveautomation.com/t/system-db-runquery-line-continuation/20103/5 "2018-10-12T10:55:47Z")

</div>

> [@paul-griffith](#):
>
> it thinks you want to replace %' using the ' format specifier

Python is quite clever with this. It only interprets `%`-signs if you also use `%` substitution with that string.

In the example shown, it's using `..." % event.source.selectedLabel` to insert a parameter into the query. If you change that query into a [`runPrepQuery`](https://docs.inductiveautomation.com/display/DOC79/system.db.runPrepQuery), you can pass the arguments in a separate array, and the `%`-signs will also be interpreted correctly.
