本文介绍了sqlite3 Connection 对象的 row_factory 方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知何故,文档对我来说并没有说得太清楚.我无法理解 sqlite3 Connection 对象的 row_factory 方法的用途.
Somehow the docs didn't make it too clear for me. I'm having trouble understanding the purpose of row_factory method of an sqlite3 Connection object.
基本上,你能解释一下下面的片段吗?
Basically, could you explain the following snippet?
def connect_db():
"""Connects to the specific database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
推荐答案
将 sqlite3.Row 分配给连接的 row_factory 的代码行创建了一些人称之为字典游标"的东西 - 它开始返回字典"而不是元组' fetchall 或 fetchone 之后的行.
The line of code assigning sqlite3.Row to the row_factory of connection creates what some people call a 'dictionary cursor', - instead of tuples it starts returning 'dictionary' rows after fetchall or fetchone.
非常标准的例子:
import sqlite3 as sqlite
conn = sqlite.connect('companies_db.sqlite')
with conn:
conn.row_factory = sqlite.Row
curs = con.cursor()
curs.execute("SELECT * FROM companies_table")
rows = curs.fetchall()
for row in rows:
print(f"{row['companyid']}, {row['name']}, {row['address']}.")
这篇关于sqlite3 Connection 对象的 row_factory 方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!