gython
gython

Reputation: 875

Writing the result of a pymssql query in a seperate list

I am reading data from a database table with pymssql. The column I am selecting contains 11 rows.

This is the code I am using:

cursor.execute('SELECT SL FROM SLM')
text = [r[0] for r in cursor.fetchall()]

However this code writes the result of the query in one list.

Is it possible to select every row seperately and write the result in 11 seperate lists?

Upvotes: 0

Views: 966

Answers (1)

Booboo
Booboo

Reputation: 44283

fetchall returns a tuple of tuples. So, if it's a list of lists you want, you need to convert each row tuples to a list:

text = [list(r) for r in cursor.fetchall()]

If you don't mind having a list of tuples, then:

text = [r for r in cursor.fetchall()]

And, of course, if you don't mind having a tuple of tuples:

text = cursor.fetchall()

If I have misunderstood what you are looking for, please clarify.

Upvotes: 1

Related Questions