Python psycopg2 不返回结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
psycopg2 not returning results
提问by bqui56
I am trying to use psycopg2
with my postgresql database just running on my local machine can't get it to return results no matter what I try. It seems to connect to the database ok, since if I alter any of the config parameters it throws errors, however, when I run seemingly valid and result worthy queries, I get nothing.
我正在尝试使用psycopg2
我的 postgresql 数据库,只是在我的本地机器上运行,无论我尝试什么,都无法让它返回结果。它似乎可以连接到数据库,因为如果我更改任何配置参数,它会引发错误,但是,当我运行看似有效且结果有价值的查询时,我什么也得不到。
My db is running and definitely has a table in it:
我的数据库正在运行,并且肯定有一个表:
postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=# select * from foos;
name | age
---------+-----
Sarah | 23
Michael | 35
Alice | 12
James | 20
John | 52
(5 rows)
My python code connects to this database but no matter what query I run, I get None
:
我的 python 代码连接到这个数据库,但无论我运行什么查询,我都会得到None
:
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("dbname='postgres' user='postgres' host='localhost'")
>>> cur = conn.cursor()
>>> print cur.execute("select * from foos;")
None
>>> print cur.execute("select * from foos")
None
>>> print cur.execute("select name from foos")
None
>>> print cur.execute("select f.name from foos f")
None
Am I doing something obviously wrong? How can I start debugging this, I don't know where to start since it connects just fine?
我做错了什么吗?我如何开始调试这个,我不知道从哪里开始,因为它连接得很好?
采纳答案by zero323
cursor.execute
prepares and executes query but doesn't fetch any data so None
is expected return type. If you want to retrieve query result you have to use one of the fetch*
methods:
cursor.execute
准备并执行查询但不获取任何数据,因此None
是预期的返回类型。如果要检索查询结果,则必须使用以下fetch*
方法之一:
print cur.fetchone()
rows_to_fetch = 3
print cur.fetchmany(rows_to_fetch)
print cur.fetchall()
回答by Andreas Jung
You did not read basic documentation which has perfect examples
您没有阅读包含完美示例的基本文档
http://initd.org/psycopg/docs/cursor.html
http://initd.org/psycopg/docs/cursor.html
>>> cur.execute("SELECT * FROM test WHERE id = %s", (3,))
>>> cur.fetchone()
(3, 42, 'bar')
回答by zzzirk
The execute()
method of a cursor simply executes the SQL that you pass to it. You then have a couple of options for getting responses from the cursor. You can use the fetchone()
method which will return the next result. In the case of the first time you call it you will get the very first result, the second time the second result and so on. The fetchall()
method returns allrows and may be used as an iterator.
execute()
游标的方法只是执行您传递给它的 SQL。然后,您有几个选项可以从光标处获取响应。您可以使用fetchone()
将返回下一个结果的方法。在第一次调用它的情况下,您将获得第一个结果,第二次获得第二个结果,依此类推。该fetchall()
方法返回所有行并可用作迭代器。
Examples:
例子:
>>> # This is an example of the fetchone() method
>>> cur.execute("select * from foos")
>>> # This call will return the first row
>>> result = cur.fetchone()
>>> # This call will return the second row
>>> result = cur.fetchone()
>>> # This is an example of the fetchall() method
>>> cur.execute("select * from foos")
>>> results = cur.fetchall()
>>> for r in results:
... print r
>>> # Now we'll reset the cursor by re-executing the query
>>> cur.execute("select * from foos")
>>> for r in cur.fetchall():
... print r
回答by John Powell
Note, as it says in the docs: http://initd.org/psycopg/docs/cursor.html"cursor objects are iterable, so, instead of calling explicitly fetchone() in a loop, the object itself can be used"
请注意,正如它在文档中所说:http: //initd.org/psycopg/docs/cursor.html“游标对象是可迭代的,因此,不是在循环中显式调用 fetchone(),而是可以使用对象本身”
Therefore, it is just as valid to write:
因此,写成同样有效:
>>> cur.execute("select foo, bar from foobars")
>>> for foo, bar in cur:
.... print foo, bar
without explicitly calling fetchone(). We pythonistas are supposed to prefer terse code, so long as it doesn't impair understanding and, imho, this feels more natural.
无需显式调用 fetchone()。我们pythonistas应该更喜欢简洁的代码,只要它不影响理解,恕我直言,这感觉更自然。