Python中sqlite3模块的row_factory参数如何使用?
下面的代码中设置了:
self.con.row_factory = dict_factory
但为什么最后未起作用呢?
import sqlite3 as sql
import os
def dict_factory(cur,row):
d = {}
for idx, col in enumerate(cur.description):
d[col[0]] = row[idx]
return d
class LiMin:
def __init__(self,ProName):
self.create_Project(ProName)
self.con.row_factory = dict_factory
def create_Project(self,ProName): # 创建或打开项目文件
self.con = sql.connect(ProName)
self.cur = self.con.cursor()
self._addTableStocks() # 添加缺省 stocks 表
self.con.commit()
def _addTableStocks(self): # 添加 stocks 表 未提交事务
self.cur.execute(‘create table if not exists stocks (name text ,number real)’)
def insertOne(self,data): # 插入一条记录
self.cur.execute(‘insert into stocks values (?,?)’,data)
if name == 'main':
dirs = os.path.dirname(__file__)
pro = LiMin(os.path.join(dirs,‘test.db’))
pro.con.row_factory = dict_factory
with pro.con:
pro.insertOne((‘abc’,33))
pro.cur.execute(‘select * from stocks’)
print(type(pro.cur.fetchone())) # 为什么不是字典?
pro.cur.close()
Python中sqlite3模块的row_factory参数如何使用?
row_factory 这玩意儿就是用来控制 sqlite3 查询结果返回格式的。默认情况下,它返回的是 tuple,用起来不太方便,特别是想通过列名访问数据的时候。设置 row_factory 可以让你自定义每一行数据的返回对象。
最常用的是把它设为 sqlite3.Row。这会把每一行变成一个“类字典”的对象,既支持通过列索引(像元组一样)访问,也支持通过列名(像字典一样)访问,而且效率比字典高。
import sqlite3
# 连接到数据库(内存数据库为例)
conn = sqlite3.connect(':memory:')
# 关键在这里:设置 row_factory
conn.row_factory = sqlite3.Row
# 创建游标,建表,插入点数据
cursor = conn.cursor()
cursor.execute('''CREATE TABLE users (id INTEGER, name TEXT, age INTEGER)''')
cursor.execute("INSERT INTO users VALUES (1, 'Alice', 30)")
cursor.execute("INSERT INTO users VALUES (2, 'Bob', 25)")
conn.commit()
# 现在查询试试
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
# 通过列名访问,清楚多了
print(f"ID: {row['id']}, Name: {row['name']}, Age: {row['age']}")
# 通过索引访问也依然可以
print(f"Same ID via index: {row[0]}")
# 它还有 .keys() 方法
print(f"Column names: {row.keys()}")
cursor.close()
conn.close()
你也可以玩得更花,自己写一个工厂函数。这个函数接收 cursor 和原始行数据(tuple)作为参数,然后返回任何你想要的东西。比如,直接返回一个字典:
import sqlite3
def dict_factory(cursor, row):
# 利用 cursor.description 获取列名
col_names = [col[0] for col in cursor.description]
# 将列名和行数据打包成字典
return dict(zip(col_names, row))
conn = sqlite3.connect(':memory:')
# 使用自定义的工厂函数
conn.row_factory = dict_factory
cursor = conn.cursor()
# ...(同样的建表、插入操作)
cursor.execute('''CREATE TABLE users (id INTEGER, name TEXT, age INTEGER)''')
cursor.execute("INSERT INTO users VALUES (1, 'Alice', 30)")
conn.commit()
cursor.execute('SELECT * FROM users')
row = cursor.fetchone()
print(row) # 输出:{'id': 1, 'name': 'Alice', 'age': 30}
print(row['name']) # 输出:Alice
cursor.close()
conn.close()
总结:想按列名访问数据,设成 sqlite3.Row 最省事。
https://google.github.io/styleguide/pyguide.html?showone=Naming#Naming 先看这个
因为你创建 cursor 是在设置 row_factory 之前,当然不起作用。

