📜  python sqlite dict - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:24.495000             🧑  作者: Mango

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

conn = sqlite3.connect("data.db")
conn.row_factory = dict_factory
db = conn.cursor()
print(db.execute("SELECT * FROM table"))
>>> [{'col1': 'val1', 'col2': 'val2'}, {'col1': 'val3', 'col2': 'val4'}]