Sqlite and Python -- return a dictionary using fetchone()?

Posted by AndrewO on Stack Overflow See other posts from Stack Overflow or by AndrewO
Published on 2009-05-01T14:19:06Z Indexed on 2010/05/15 23:50 UTC
Read the original article Hit count: 267

Filed under:
|
|

I'm using sqlite3 in python 2.5. I've created a table that looks like this:

   create table votes (
      bill text,
      senator_id text,
      vote text)

I'm accessing it with something like this:

v_cur.execute("select * from votes")
row = v_cur.fetchone()
bill = row[0]
senator_id = row[1]
vote = row[2]

What I'd like to be able to do is have fetchone (or some other method) return a dictionary, rather than a list, so that I can refer to the field by name rather than position. For example:

bill = row['bill'] 
senator_id = row['senator_id']
vote = row['vote']

I know you can do this with MySQL, but does anyone know how to do it with SQLite?

Thanks!!!

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlite