为什么 IPython 在向 sqlite 提交查询时无法返回具有多个字段的记录?

发布于 2024-07-29 01:04:28 字数 886 浏览 9 评论 0原文

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

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

发布评论

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

评论(5

情感失落者 2024-08-05 01:04:28

这就是 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.)

年华零落成诗 2024-08-05 01:04:28

您可以尝试更多的调试:

s = 'SELEECT * from studies'
print s
conn.execute(s).fetchall()

或者:

s = 'SELECT ' + chr(42) + ' from studies'
conn.execute(s).fetchall()

您也可以尝试:

conn.execute('select count(*) from studies').fetchall()

如果返回为 [(0,)],那么发生了一些非常奇怪的事情:-)


您可以尝试更多的事情:

conn.execute('select id from (select * from studies)').fetchall()

或者:

cur = conn.cursor()
cur.execute('select * from studies').fetchall()

Some more debugging you could try:

s = 'SELEECT * from studies'
print s
conn.execute(s).fetchall()

or:

s = 'SELECT ' + chr(42) + ' from studies'
conn.execute(s).fetchall()

You might also try:

conn.execute('select count(*) from studies').fetchall()

if that comes back as [(0,)] then something really weird is going on :-)


Some more things you could try:

conn.execute('select id from (select * from studies)').fetchall()

or:

cur = conn.cursor()
cur.execute('select * from studies').fetchall()
琉璃梦幻 2024-08-05 01:04:28

我已经尝试了您在 IPython 和 sqlite 中提到的所有内容,没有任何问题(ipython 0.9.1,python 2.5.2)。

这是否有可能是某种版本不匹配问题? 也许您的 shell 引用了不同的库?

例如,

import sqlite3; print sqlite3.version

两个 shell(即 ipython 和 sql 查询工作的常规 shell)是否返回相同的内容?

怎么样?

conn.execute('select sqlite_version()').fetchall()

那返回同样的结果

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

import sqlite3; print sqlite3.version

return the same thing from both shells (i.e. ipython and the regular one where the sql query works)?

How about

conn.execute('select sqlite_version()').fetchall()

Does that return the same thing?

叶落知秋 2024-08-05 01:04:28

只是一个大胆的猜测,但请尝试转义数据库文件路径中的反斜杠。 换句话说,而不是

db = 'G:\path\to\db\file.sqlite'

尝试

db = 'G:\\path\\to\\db\\file.sqlite'

Just a wild guess, but please try to escape backslashes in the path to the database file. In other words instead of

db = 'G:\path\to\db\file.sqlite'

try

db = 'G:\\path\\to\\db\\file.sqlite'
对不⑦ 2024-08-05 01:04:28

我遇到了类似的问题,发现获得正确输出的唯一方法是将执行语句的结果分配给另一个游标对象并调用 fetchall :

In [1]: import sqlite3
In [2]: conn = sqlite3.connect('file.db')
In [3]: cur = conn.cursor()
In [4]: cur2 = cur.execute("<STATEMENT>")
In [5]: cur2.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:

In [1]: import sqlite3
In [2]: conn = sqlite3.connect('file.db')
In [3]: cur = conn.cursor()
In [4]: cur2 = cur.execute("<STATEMENT>")
In [5]: cur2.fetchall()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文