gpt4 book ai didi

python - 使用 sqlite3.Row 按名称索引结果的行为不一致

转载 作者:行者123 更新时间:2023-11-28 18:55:14 32 4
gpt4 key购买 nike

在我的 Python 应用程序中,我一直使用 sqlite3.Row 作为行工厂来按名称索引结果一段时间,没有出现任何问题。最近我将我的应用程序移到了一个新服务器(没有代码更改),我发现这种索引方法现在在新服务器上出乎意料地在特定条件下失败了。我看不到任何解释。

当我的选择查询中有 DISTINCT 关键字时,问题似乎出现在新服务器上:

import sqlite3
conn = sqlite3.connect(':memory:')
conn.row_factory = sqlite3.Row
c = conn.cursor()

c.execute('create table test ([name] text)')
c.execute("insert into test values ('testing')")
conn.commit()

c.execute('select [name] from test')
row = c.fetchone()
print row['name'] # works fine on both machines

c.execute('select distinct [name] from test') # add distinct keyword
row = c.fetchone()
print row['name'] # fails on new server (no item with that key)

如您所见,我可以使用内存数据库对这个问题进行沙盒处理,所以这个问题与我现有的数据无关。两台机器都基于 Debian(旧版本:Ubuntu 8.10,新版本:Debian 5.0.3)并且两台机器都运行 Python 2.5.2。我相信 sqlite3 模块是 Python 安装的核心部分,所以我不知道这种微妙的破坏是如何发生的,因为 python 版本是相同的。

有没有人有任何想法,或者以前见过这样的东西?

谢谢,
克里斯

最佳答案

尝试添加行

print row.keys()

而不是“print row['name']”来查看第 0 列在第二种情况下的实际名称(它可能被“DISTINCT”关键字更改)。

或者,您可以在这种情况下使用 row[0],但这很可能不是您想要的。 :)

关于python - 使用 sqlite3.Row 按名称索引结果的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3805785/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com