[Tutor] trouble with odbc
Brad Chandler
mbc2@netdoor.com
Thu, 29 Mar 2001 16:45:39 -0600
----- Original Message -----
From: "Daniel Coughlin" <kauphlyn@speakeasy.org>
To: <tutor@python.org>
Sent: Thursday, March 29, 2001 12:05 PM
Subject: Re: [Tutor] trouble with odbc
>
>
> > >>> db = odbc.odbc('northwind/sa')
> > >>> cur = s.cursor()
> Oops! this line is actually
> >>>cur = db.cursor()
> I typed the wrong code into my email message. Sorry for any confusion.
>
> > >>> cur.execute('select * from Customers')
> > >>> print cur.description
> > >>> while 1:
> > >>> rec = cur.fetchall()
> > >>> if not rec: break
> > >>> print rec
Well, I'm not sure why yours isn't working, it may have something to do with
the way your looping through your records. I'm just guessing, but that "if"
statement may be causing the problem. I like to say something like:
###Begin Code
cur.execute('select * from Customers')
print cur.description
result = cur.fetchall()
numrows = len(result) # the number of rows returned by the query
for x in range(0,numrows):
numfields = len(result[x]) # the number of fields in each row
for y in range(0,numfields):
print result[x][y],
print
###End Code
This should print out each field in a row on a single line. That last print
statement should add a break between rows. I haven't tested that exact
code, but I use something like that whenever I query a database. I'm not
saying that's the best method, but maybe it'll give you some ideas.
Brad