为什么 IPython 在向 sqlite 提交查询时无法返回具有多个字段的记录?
我正在尝试在 python 脚本中编写一个对 sqlite 数据库的简单查询。 为了测试我的参数是否正确,我尝试从 ipython 命令行运行查询。 它看起来像这样:
import sqlite3
db = 'G:\path\to\db\file.sqlite'
conn = sqlite3.connect(db)
results = conn.execute('SELECT * FROM studies').fetchall()
由于某种原因,我的结果完全是空的。 然后我尝试了另一个测试查询:
results = conn.execute('SELECT id FROM studies').fetchall()
它返回正确。 我认为星号有问题[错误,请参阅下面的第二次更新],因此我尝试从默认的 python 命令行执行“SELECT * FROM Studies”查询。 你瞧,它正确返回了。 我尝试了所有正常的方法来逃避星号,结果却遇到了各种各样的错误消息。 有没有办法在 IPython 中运行这个查询?
编辑:抱歉,我错误地认为 IronPython 和 IPython 是相同的。 我的意思是 IPython 命令行,而不是 IronPython 框架。
编辑2:好的,事实证明星号确实有效,如这个成功的查询所示:
'SELECT COUNT(*) FROM studies'
从此处发布的建议来看,错误结果是尝试返回具有多个字段的记录,即:
'SELECT field1,field2 FROM studies'
这仍然导致记录被返回。 我相应地更改了问题的标题。
I am trying to write a simple query to an sqlite database in a python script. To test if my parameters were correct, I tried running the query from the ipython command line. It looked something like this:
import sqlite3
db = 'G:\path\to\db\file.sqlite'
conn = sqlite3.connect(db)
results = conn.execute('SELECT * FROM studies').fetchall()
for some reason, my results came back totally empty. Then I tried another test query:
results = conn.execute('SELECT id FROM studies').fetchall()
Which returned correctly. I figured there was a problem with the asterisk [WRONG, SEE SECOND UPDATE BELOW], so I tried the 'SELECT * FROM studies' query from a default python command line. Lo and behold, it returned correctly. I tried all the normal ways to escape the asterisk only to be met by a wide variety of error messages. Is there any way to run this query in IPython?
EDIT: Sorry, I incorrectly assumed IronPython and IPython were the same. What I meant was the IPython command line, not the IronPython framework.
EDIT2: Okay, it turns out the asterisk DOES work as shown by this successful query:
'SELECT COUNT(*) FROM studies'
From the suggestions posted here, it turns out the error results from trying to return records with multiple fields, i.e.:
'SELECT field1,field2 FROM studies'
which still results in to records being returned. I have changed the title of the question accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是 SQL。 IronPython 与查询处理几乎没有任何关系。 您是否使用了不寻常的字符编码? (IE 不是 UTF-8 或 ASCII)?
如果您
从研究中选择 id,fieldname,fieldname
会发生什么(换句话说,模拟“*”的作用。)This is SQL. IronPython has little or nothing to do with the processing of the query. Are you using an unusual character encoding? (IE not UTF-8 or ASCII)?
What happens if you
SELECT id,fieldname,fieldname FROM studies
(In other words, simulating what '*' does.)您可以尝试更多的调试:
或者:
您也可以尝试:
如果返回为 [(0,)],那么发生了一些非常奇怪的事情:-)
您可以尝试更多的事情:
或者:
Some more debugging you could try:
or:
You might also try:
if that comes back as [(0,)] then something really weird is going on :-)
Some more things you could try:
or:
我已经尝试了您在 IPython 和 sqlite 中提到的所有内容,没有任何问题(ipython 0.9.1,python 2.5.2)。
这是否有可能是某种版本不匹配问题? 也许您的 shell 引用了不同的库?
例如,
两个 shell(即 ipython 和 sql 查询工作的常规 shell)是否返回相同的内容?
怎么样?
那返回同样的结果
I've tried all the things you've mentioned in IPython and sqlite without any problems (ipython 0.9.1, python 2.5.2).
Is there a chance this is some kind of version mismatch issue? Maybe your shells are referencing different libraries?
For example, does
return the same thing from both shells (i.e. ipython and the regular one where the sql query works)?
How about
Does that return the same thing?
只是一个大胆的猜测,但请尝试转义数据库文件路径中的反斜杠。 换句话说,而不是
尝试
Just a wild guess, but please try to escape backslashes in the path to the database file. In other words instead of
try
我遇到了类似的问题,发现获得正确输出的唯一方法是将执行语句的结果分配给另一个游标对象并调用 fetchall :
I had a similar problem and found the only way to get the correct output was by assigning the results of the execute statement to another cursor object and calling fetchall on that: