相关文章推荐
曾经爱过的小蝌蚪  ·  debugging - What is ...·  1 年前    · 
个性的韭菜  ·  android - Show GIF ...·  1 年前    · 
面冷心慈的枕头  ·  C# ...·  1 年前    · 


通过设置连结的​ ​row_factory​ ​,可以自定义返回列表样式,常用的是返回字典列表。

import sqlite3

def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d

con = sqlite3.connect(":memory:")
con.row_factory = dict_factory
cur = con.cursor()
cur.execute("select 1 as a")
print cur.fetchone()["a"]

参考:
​​ ​ https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.row_factory​