psycopg2没有返回结果

bqu*_*i56 14 python postgresql psycopg2

我试图使用psycopg2我的postgresql数据库只是在我的本地机器上运行无法让它返回结果无论我尝试什么.它似乎连接到数据库确定,因为如果我改变任何配置参数它会抛出错误,但是,当我运行看似有效和结果有价值的查询时,我什么也得不到.

我的数据库正在运行,并且肯定有一个表:

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)
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?我怎么能开始调试这个,我不知道从哪里开始,因为它连接得很好?

zer*_*323 19

cursor.execute准备并执行查询但不获取任何数据,因此None是预期的返回类型.如果要检索查询结果,则必须使用以下fetch*方法之一:

print cur.fetchone()

rows_to_fetch = 3
print cur.fetchmany(rows_to_fetch)

print cur.fetchall()
Run Code Online (Sandbox Code Playgroud)