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

后端 未结 8 1727
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  庸人自扰
    2021-01-31 04:18

    I've used this:

    def get_dict(sql):
        return dict(c.execute(sql,()).fetchall())
    

    Then you can do this:

    c = conn.cursor()
    d = get_dict("select user,city from vals where user like 'a%'");
    

    Now d is a dictionary where the keys are user and the values are city. This also works for group by

提交回复
热议问题