# Grab string from SQL database

**URL:** <https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997>\
**Category:** Ignition\
**Created:** [August 13, 2012, 8:33pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997 "2012-08-13T20:33:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [August 13, 2012, 8:33pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/1 "2012-08-13T20:33:45Z")

</div>

I’m trying to grab a value from a table like so

`groupName = system.db.runPrepQuery("SELECT DISTINCT groupname FROM shipping_stores WHERE store LIKE ? AND chain LIKE ?", [(valueMod), (value2)], 'Sample')
	value4 ="There is already '%s' going to chain '%s' in group '%s'." % (valueMod, value2, groupName)`  
and spit out a string which uses this value.  
I cant seem to coerce what looks like a pydataset to a single string value.  
Any idea what I’m missing here?

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [August 13, 2012, 9:11pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/2 "2012-08-13T21:11:41Z")

</div>

I assume the query returns a single value, right? You can use this to get the element 0,0 in the dataset:

```auto
groupName[0][0]
```

---

<div class="post-metadata">

**Author:** ![dave.fogle](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dave.fogle](https://forum.inductiveautomation.com/u/dave.fogle)\
**Post date:** [August 13, 2012, 9:13pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/3 "2012-08-13T21:13:55Z")

</div>

You can use system.db.runScalarQuery() which will return the value located in the first row and column of your resultset. [inductiveautomation.com/support/ ... rquery.htm](http://inductiveautomation.com/support/usermanuals/ignition/system_db_runscalarquery.htm)

If you have more than one value in the resultset that you want to use you'll have to loop through the pydataset and pull out the values you want.

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [August 16, 2012, 3:17pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/4 "2012-08-16T15:17:46Z")

</div>

I’m trying to write a loop for this like

[code]value1 = event.source.parent.getComponent(‘GroupName1’).text  
divertID = event.source.parent.divertID  
bool = false

results = system.db.runPrepQuery(“SELECT distinct groupname FROM shipping\_stores WHERE divert LIKE ?”, [divertID], ‘Sample’)

for i in results[][]:  
if results[i] == value1:  
bool = 1  
break

if bool == 1:  
…:[/code]

Is this a good way to set it up? I’m getting some errors with my syntax

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 16, 2012, 4:10pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/5 "2012-08-16T16:10:04Z")

</div>

It’d need to be more like this, I think:

```auto
for i in results:
   if i[0] == value1:
      bool = 1
      break
```

In this instance, i is a row in the dataset. The [0] would be the first value in the row.

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [August 16, 2012, 4:38pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/6 "2012-08-16T16:38:51Z")

</div>

thanks Jordan,

update:

`for i in range(len(results)):
	if results[i][0] == value1:
		bool = 1
		break`  
It’s a single column dataset.  
It works now.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [August 16, 2012, 4:47pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/7 "2012-08-16T16:47:58Z")

</div>

Great, glad you got it working.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 16, 2012, 5:09pm UTC](https://forum.inductiveautomation.com/t/grab-string-from-sql-database/4997/8 "2012-08-16T17:09:57Z")

</div>

Nice! 😃
