Home > Article > Backend Development > Introduction to the method of returning query results in dictionary form in Python Sqlite3
SQLite3 itself does not natively provide dictionary cursors like pymysql.
cursor = conn.cursor(pymysql.cursors.DictCursor)
But the corresponding implementation plan has been reserved in the official documents.
def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d
Use this function instead of the conn.raw_factory attribute.
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"]
The above is the detailed content of Introduction to the method of returning query results in dictionary form in Python Sqlite3. For more information, please follow other related articles on the PHP Chinese website!