[newbie] Right way to access item in array?

Steve Holden steve at holdenweb.com
Tue Oct 28 09:56:11 EDT 2008


Gilles Ganault wrote:
> 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
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list