Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55081 > unrolled thread

fetchall to python lists

Started bychristensen.jerome@gmail.com
First post2013-09-30 07:58 -0700
Last post2013-09-30 08:16 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  fetchall to python lists christensen.jerome@gmail.com - 2013-09-30 07:58 -0700
    Re: fetchall to python lists Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-30 11:10 -0400
    Re: fetchall to python lists christensen.jerome@gmail.com - 2013-09-30 08:16 -0700

#55081 — fetchall to python lists

Fromchristensen.jerome@gmail.com
Date2013-09-30 07:58 -0700
Subjectfetchall to python lists
Message-ID<812b0b0b-fa73-4fc3-b8d0-ac0a9c93207e@googlegroups.com>
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

[toc] | [next] | [standalone]


#55085

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-09-30 11:10 -0400
Message-ID<mailman.494.1380553832.18130.python-list@python.org>
In reply to#55081

[Multipart message — attachments visible in raw view] — view raw

On Mon, Sep 30, 2013 at 10:58 AM, <christensen.jerome@gmail.com> wrote:

> 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

--
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#55086

Fromchristensen.jerome@gmail.com
Date2013-09-30 08:16 -0700
Message-ID<96293b0a-3a98-4f23-a4cd-0229a15328ab@googlegroups.com>
In reply to#55081
On Monday, September 30, 2013 4:58:25 PM UTC+2, christens...@gmail.com wrote:
> 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!!!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web