fetchall to python lists

C

christensen.jerome

Hi - I have some basic programming experience and new to Python. I have connected to SQL Server as follows:

import pyodbc
conn = pyodbc.connect('DSN=DBC')
cursor = conn.cursor()
cursor.execute("select measure,fin_year_no,fin_week_no,location_no,value from actual")
result=cursor.fetchall()

result looks like this:



result[0] - ('2013', 2014, 7, 242, 96064.35)
result[1] - ('2013', 2014, 7, 502, 18444.2)
..... approximately 2m records

Is there a way to assign the values of result to 5 lists without doing 5 select statments one for each of the colums and then assigning it to a list so that:

list1[0] = '2013'
list1[1] = 2014
list1[2] = 7
list1[3] = 242
list1[4] = 96064.35

list2[0] = '2013'
list2[1] = 2014
list2[2] = 7
list2[3] = 502
list2[4] = 18444.2

and so on ...

Hope someone can help. Regards Jerome
 
J

Joel Goldstick

Hi - I have some basic programming experience and new to Python. I have
connected to SQL Server as follows:

import pyodbc
conn = pyodbc.connect('DSN=DBC')
cursor = conn.cursor()
cursor.execute("select measure,fin_year_no,fin_week_no,location_no,value
from actual")
result=cursor.fetchall()

result looks like this:



result[0] - ('2013', 2014, 7, 242, 96064.35)
result[1] - ('2013', 2014, 7, 502, 18444.2)
.... approximately 2m records

Is there a way to assign the values of result to 5 lists without doing 5
select statments one for each of the colums and then assigning it to a list
so that:
What you have below is just result[0][0], result[0][1], etc.


list1[0] = '2013'
list1[1] = 2014
list1[2] = 7
list1[3] = 242
list1[4] = 96064.35

list2[0] = '2013'
list2[1] = 2014
list2[2] = 7
list2[3] = 502
list2[4] = 18444.2

and so on ...

Hope someone can help. Regards Jerome

So what I'm trying to say is that you already have what you want. each
tuple is contained in the out list of all of the tuples.

For brevity sake, I am acting as if the data set contained only a single
row:
result = (('2013', 2014, 7, 242, 96064.35),)
result (('2013', 2014, 7, 242, 96064.35),)
result[0] ('2013', 2014, 7, 242, 96064.35)
result[0][0] '2013'
result[0][1]
2014
 
C

christensen.jerome

Hi - I have some basic programming experience and new to Python. I have connected to SQL Server as follows:



import pyodbc

conn = pyodbc.connect('DSN=DBC')

cursor = conn.cursor()

cursor.execute("select measure,fin_year_no,fin_week_no,location_no,value from actual")

result=cursor.fetchall()



result looks like this:







result[0] - ('2013', 2014, 7, 242, 96064.35)

result[1] - ('2013', 2014, 7, 502, 18444.2)

.... approximately 2m records



Is there a way to assign the values of result to 5 lists without doing 5 select statments one for each of the colums and then assigning it to a list so that:



list1[0] = '2013'

list1[1] = 2014

list1[2] = 7

list1[3] = 242

list1[4] = 96064.35



list2[0] = '2013'

list2[1] = 2014

list2[2] = 7

list2[3] = 502

list2[4] = 18444.2



and so on ...



Hope someone can help. Regards Jerome

Thanks Joel did not think it could be so simple!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top