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

后端 未结 8 1725
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 04:03

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)
<         


        
8条回答
  •  旧时难觅i
    2021-01-31 04:08

    There is actually an option for this in sqlite3. Change the row_factory member of the connection object to sqlite3.Row:

    conn = sqlite3.connect('db', row_factory=sqlite3.Row)
    

    or

    conn.row_factory = sqlite3.Row
    

    This will allow you to access row elements by name--dictionary-style--or by index. This is much more efficient than creating your own work-around.

提交回复
热议问题