# Cleaner way to get results of a query into variable names that match column?

**URL:** <https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [November 8, 2021, 7:34pm UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845 "2021-11-08T19:34:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 8, 2021, 7:34pm UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/1 "2021-11-08T19:34:28Z")

</div>

Calling all code-golfers (@paul-griffith I know you like these)

I am currently rewriting some stored procedures from the database into jython (to try to prove to my coworkers it’s a much easier to read 100 lines of jython vs 400 lines of SQL Server syntax to do the same thing).

Here is the situation I am trying to make a bit cleaner/closer to one line

```auto
		productData = system.db.runQuery("SELECT col1, col2, col3, col4 FROM table WHERE id = '%i'"%(recordId))
		col1 ,col2, col3, col4 = productData.getValueAt(0, 'col1'), productData.getValueAt(0, 'col2'), productData.getValueAt(0, 'col3'), productData.getValueAt(0, 'col4')
		print col1, col2, col3, col4

```

That second line is really the problematic one as I want to do the same thing for certain queries that might have 10 or more columns I can see using a list to populate the `SELECT` statement, but I don’t know how I could go from that, to dynamically making and assigning values to variables.

Not even sure if this is a good idea, but I would like to know if it’s possible as a proof of concept - to be bale to quickly to go from `results = system.db.runQuery("SELECT col1, col2, .. colN FROM table WHERE id = %i"%(recordId))` to having variables with the names `col1, col2, ... colN` with the associated value from the database.

You can assume the query will only return a single row (the only case where it would be sensible to try to do this).

Anyone know how to do this?

---

<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:** [November 8, 2021, 7:39pm UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/2 "2021-11-08T19:39:32Z")

</div>

It’s not really practical to dynamically assign variables like this, but the obvious thing to try seems like returning a dictionary? So you’d have to `productData['col1']` - still some extra characters, but not much.

You’d have to decide how to feed the columns into this query, which would define your expected result.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 8, 2021, 7:42pm UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/3 "2021-11-08T19:42:18Z")

</div>

Yea I have a dictionary function already that does similar. I love dictionaries but not everyone I work with does, hence the question. This was more wondering if it was possible due to this reason. Good to know. Thanks @paul-griffith

---

<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:** [November 9, 2021, 3:18pm UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/4 "2021-11-09T15:18:16Z")

</div>

8 posts were split to a new topic: [Goofin’ about keyboards](https://forum.inductiveautomation.com/t/goofin-about-keyboards/52879)

---

<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:** [November 9, 2021, 1:37am UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/6 "2021-11-09T01:37:42Z")

</div>

I use a wrapper for dictionaries that look up missing properties in the dictionary, with an automatic fallback to none. `SmartMap` In this [util script](https://www.automation-pros.com/ignition/smartmap.py) module.

I instanciate from a dataset `ds` like so:

```auto
smap = shared.smartmap.SmartMap(zip(ds.columnNames, [ds.getValueAt(0, c) for c in range(ds.columnCount)]))

```

Then you can use `smap.ColumnName` in your code.

---

<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:** [November 9, 2021, 10:39am UTC](https://forum.inductiveautomation.com/t/cleaner-way-to-get-results-of-a-query-into-variable-names-that-match-column/52845/12 "2021-11-09T10:39:58Z")

</div>

> [@bkarabinchak.psi](#):
>
> `col1 ,col2, col3, col4 = productData.getValueAt(0, 'col1'), productData.getValueAt(0, 'col2'), productData.getValueAt(0, 'col3'), productData.getValueAt(0, 'col4')`

not really a super good idea but it works... aslong there are no spaces or weird characters

```python
for name in productData.getColumnNames():
	 exec("%s = productData[0][name]"%name)

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/1/713af2b6488d9121e8ced4292be145de2e3791dc.png)
