[newbie] Right way to access item in array?

G

Gilles Ganault

Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw.Connection("test.sqlite")
cursor=connection.cursor()

rows=cursor.execute("SELECT isbn,price FROM books WHERE price IS
NULL")
for row in rows:

#Is this right?
for isbn in row:

if isbn:
print "Found price = " + price

connection.close(True)
=====

Thank you.
 
D

Diez B. Roggisch

Gilles said:
Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw.Connection("test.sqlite")
cursor=connection.cursor()

rows=cursor.execute("SELECT isbn,price FROM books WHERE price IS
NULL")
for row in rows:

#Is this right?
for isbn in row:

No. This will iterate though all columns, not just the isbn.

You can use tuple-unpacking in such cases:

for row in rows:
isbn, price = row
...


Diez
 
G

Gerhard Häring

Diez said:
No. This will iterate though all columns, not just the isbn.

You can use tuple-unpacking in such cases:

for row in rows:
isbn, price = row
...

You can do it even in one step with APSW (and pysqlite, and others):

for isbn, price in cur.execute("select isbn, price ..."):
....

-- Gerhard
 
S

Steve Holden

Gilles said:
Hello

I'd like to know what the right way is to access an item in a row as
returned by a database:

=====
import apsw

connection=apsw.Connection("test.sqlite")
cursor=connection.cursor()

rows=cursor.execute("SELECT isbn,price FROM books WHERE price IS
NULL")

If you are dealing with a DB API-compliant module then the return value
from the cursor's execute method is undefined, and you need to call one
of the "fetch" methods to extract the retrieved data.

So you would want something like

cursor.execute("SELECT isbn,price FROM books WHERE price IS NULL")
rows = cursor.fetchall()
for isbn, price in rows:
print isbn, ":", price

Once you get that working you can do your own computations with isbn and
price. Note, however, that the specific query you use guarantees that
the value of "proce" will be None, since you only retrieve the rows
where price is NULL!
for row in rows:

#Is this right?
for isbn in row:

if isbn:
print "Found price = " + price

connection.close(True)
regards
Steve
 
G

Gilles Ganault

You can do it even in one step with APSW (and pysqlite, and others):

for isbn, price in cur.execute("select isbn, price ..."):

Thanks much guys. For those interested, here's some working code:

======
import apsw

connection=apsw.Connection("mybooks.sqlite")
cursor=connection.cursor()

for id, isbn in list(cursor.execute("SELECT id,isbn FROM books WHERE
price IS NULL")):

print isbn

connection.close(True)
======

HTH,
 
G

Gilles Ganault

If you are dealing with a DB API-compliant module then the return value
from the cursor's execute method is undefined, and you need to call one
of the "fetch" methods to extract the retrieved data.

Thanks for pointing it out. I read that calling list() on the dataset
is the way to go with this module:

for id, isbn in list(cursor.execute("SELECT id,isbn FROM books WHERE
price IS NULL")):
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top