在 apsw 上重现 pysqlite 的 row_factory
我一直在尝试从 pysqlite 迁移到 apsw 但我不能 找到一种方法来重现其 row_factory 函数。
这是我的原始代码:
connection = sqlite3.connect("db.db3")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
我这样使用它:
query = """ SELECT wbcode, Year, """+query_name+"""
FROM innovotable WHERE commodity='"""+commodity_name+"""' and
"""+query_name+""" != 'NULL' """
rows = cursor.execute(query)
for row in rows:
s[str(row[0])+str(row[1])] = float(row[2])
是否可以将 apsw 用于相同的目的?
I have been trying to migrate away from pysqlite to apsw but I can't
find a way to reproduce its row_factory function.
this is my original code:
connection = sqlite3.connect("db.db3")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
and I use it like this:
query = """ SELECT wbcode, Year, """+query_name+"""
FROM innovotable WHERE commodity='"""+commodity_name+"""' and
"""+query_name+""" != 'NULL' """
rows = cursor.execute(query)
for row in rows:
s[str(row[0])+str(row[1])] = float(row[2])
Is it possible to use apsw for this same purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
披露:我是 APSW 的作者
是的,非常容易。 APSW 有一个称为行跟踪器的东西,记录在 http://apidoc.apsw.googlecode.com/hg/execution .html#tracing
每行都会调用行跟踪器。您可以通过返回 None 来完全跳过该行,或者根据传入的内容创建您想要的任何数据类型(这非常适合测试)。如果行跟踪器安装在连接上,那么它会影响所有游标。如果位于光标上,则仅该光标受到影响。 Cursor.getdescription 将让您获取列名和声明的类型。
上面的代码实际上并没有使用 row_factory,因为您按数字索引行,这在 pysqlite 和 APSW 中是有效的。 sqlite3.Row 确实允许您按名称进行索引,因此最后一行代码将是:
顺便说一句,还有一个由 pysqlite 作者和我自己运行的 Python SQLite 组,链接到 pysqlite 和 APSW 站点。
Disclosure: I am the author of APSW
Yes, very easily. APSW has something called a row tracer documented at http://apidoc.apsw.googlecode.com/hg/execution.html#tracing
The row tracer is called with each row. You can skip the row completely by returning None, or make whatever data type you want based on what was passed in (this is great for testing). If the row tracer is installed on a Connection then it affects all cursors. If on a cursor then only that cursor is affected. Cursor.getdescription will let you get the column names and declared types.
Your code above doesn't actually use the row_factory as you index the row by number which works as is in pysqlite and APSW. sqlite3.Row does let you index by name so the last line of code would be:
BTW there is also a Python SQLite group run by the pysqlite author and myself linked to from the pysqlite and APSW sites.
我在以下方面取得了良好的结果:
I've had good results with: