# Proper system.db.runNamedQuery usage in python

**URL:** <https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768>\
**Category:** Ignition\
**Created:** [November 7, 2019, 8:26pm UTC](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768 "2019-11-07T20:26:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyrel.parker](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@tyrel.parker](https://forum.inductiveautomation.com/u/tyrel.parker)\
**Post date:** [November 7, 2019, 8:26pm UTC](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768/1 "2019-11-07T20:26:16Z")

</div>

I frequently have troubles where I want to run a named query within a python script, but when I try to iterate through that dataset to format the returns I end up crashing my designer. I don’t think I am doing anything that crazy, but I am wondering if there is a best practices for handling the returned datasets in the designer?

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c337d6380c8cc494c2a6eb8b291fa16884a89293.png)

This is the result of the Query in MSSMS, and I just want to manipulate those two results into a singular string formatted how I want. The command I’m currently using in my script is:

roles = system.dataset.toPyDataSet(system.db.runNamedQuery(self.session.custom.ProjectName, “UserManagement/Get Roles for a UserName”, {‘UserName’:row.get(‘username’)}))

and I want the output to be “Supervisor, test4”.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [November 7, 2019, 8:50pm UTC](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768/2 "2019-11-07T20:50:08Z")

</div>

```auto
# Not a great example, but (something very much like) this works for me
roles = system.dataset.toPyDataSet(system.db.runNamedQuery(“UserManagement/Get Roles for a UserName”, {‘UserName’:row.get(‘username’)}))
entries = []
for i in range (len(roles)):
    entries.append(roles[i][0])
return ', '.join(map(str, entries))

```

You should note that in my example I’m using 8.0.6, so runNamedQuery no longer requires a project name argument. I have no idea how you’re acquiring the `row` variable you’re using to get the user’s name, and so I didn’t use arguments in my query. My result came back as  
`"Supervisor, OtherRole, Third"`.

If this isn’t helpful, could you provide any stacktrace you’ve come across when your Designer crashes?

---

<div class="post-metadata">

**Author:** ![tyrel.parker](https://avatars.discourse-cdn.com/v4/letter/t/47e85d/32.png) [@tyrel.parker](https://forum.inductiveautomation.com/u/tyrel.parker)\
**Post date:** [November 8, 2019, 10:34pm UTC](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768/3 "2019-11-08T22:34:23Z")

</div>

I was using

```auto
for role in roles:
    entries.append(role)

```

to loop through and create my string, and it kept crashing.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [November 8, 2019, 10:49pm UTC](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768/4 "2019-11-08T22:49:37Z")

</div>

Right, so the `roles` list was a list of PyDataSet rows, not Strings, so it was attempting to join a bunch of items which weren’t strings with an actual string, and I’m not sure if PyDataSet rows have any way to toString themselves because they could contain anything.

The solution I provided is pretty fragile, but hopefully it got you to a spot where you could continue…
