rowcount in dbi w/ odbc

don donald.braman at yale.edu
Thu Apr 4 20:37:57 EST 2002


I'm new to Python programming. I am trying to gain access to specific
columns in specific rows in an Access database, but am not having much luck.
I thought I'd first try to access specific rows and columns. However, when I
try to use "rowcount" to see how many rows there are, I get "AttributeError:
rowcount". I assume there is some easy way to do this, but I can't seem to
figure it out. Here's my attempt that fails:

import dbi, odbc
try:
        con = odbc.odbc('mydatabase')
        cur = con.cursor()
        cur.execute('SELECT * FROM mytable')
        con.close()

        for row in range(0, cur.rowcount()-1):
                for col in range(0, cur.colCount()-1):
                        print cur.getField(row, col), ',',
                print cur.getField(row, col)

except NameError,e:
        print 'error ', e, 'undefined'


I suppose I should note that I can access the data using the following, but
that it doesn't give me access to a specific rows:

import dbi, odbc
try:
    s = odbc.odbc('mydatabase')
    cur = s.cursor()
    cur.execute('SELECT * FROM mytable)
    print cur.description
    for tup in cur.description:
        print tup[0],
    print
    while 1:
        rec = cur.fetchmany(1)
        if not rec: break
        print rec

except NameError,e:
        print 'error ', e, 'undefined'

-don





More information about the Python-list mailing list