[newbie] Right way to access item in array?

Gerhard Häring gh at ghaering.de
Tue Oct 28 12:12:26 CET 2008


Diez B. Roggisch wrote:
> 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")
>> 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
>    ...

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

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

-- Gerhard




More information about the Python-list mailing list