在 apsw 上重现 pysqlite 的 row_factory

发布于 2024-09-26 02:47:45 字数 550 浏览 3 评论 0原文

我一直在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城缀染半城烟沙 2024-10-03 02:47:45

披露:我是 APSW 的作者

是的,非常容易。 APSW 有一个称为行跟踪器的东西,记录在 http://apidoc.apsw.googlecode.com/hg/execution .html#tracing

每行都会调用行跟踪器。您可以通过返回 None 来完全跳过该行,或者根据传入的内容创建您想要的任何数据类型(这非常适合测试)。如果行跟踪器安装在连接上,那么它会影响所有游标。如果位于光标上,则仅该光标受到影响。 Cursor.getdescription 将让您获取列名和声明的类型。

上面的代码实际上并没有使用 row_factory,因为您按数字索引行,这在 pysqlite 和 APSW 中是有效的。 sqlite3.Row 确实允许您按名称进行索引,因此最后一行代码将是:

s[str(row.wbcode)+str(row.Year)]=float(row[query_name])

顺便说一句,还有一个由 py​​sqlite 作者和我自己运行的 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:

s[str(row.wbcode)+str(row.Year)]=float(row[query_name])

BTW there is also a Python SQLite group run by the pysqlite author and myself linked to from the pysqlite and APSW sites.

半衬遮猫 2024-10-03 02:47:45

我在以下方面取得了良好的结果:

connection.setrowtrace(row_factory)

def row_factory(cursor, row):
    columns = [t[0] for t in cursor.getdescription()]
    return dict(zip(columns, row))

I've had good results with:

connection.setrowtrace(row_factory)

def row_factory(cursor, row):
    columns = [t[0] for t in cursor.getdescription()]
    return dict(zip(columns, row))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文