如何调试cursor.execute(psycopg2)是NoneType

时间:2015-08-28 20:09:29

标签: python postgresql psycopg2

我尝试对我的postgres db运行SQL,

我通过的连接对象

import psycopg2
conn_string = "host='localhost' port='5432' dbname='postgres' user='postgres' password='mysecretpassword'"
conn = psycopg2.connect(conn_string)

似乎没问题

result = cursor.execute(
"""
select 
* 
from 
planet_osm_point limit 10
""")

结果是非类型,所以一定是出错了吗?

我做错了什么?我该怎么调试呢?

1 个答案:

答案 0 :(得分:3)

cursor.execute()仅执行查询,不会获取任何数据。要接收数据,您需要致电cursor.fetchall()cursor.fetchone()

import psycopg2
conn_string = "host = 'localhost' port = '5432' dbname = 'postgres' user = 'postgres' password = 'mysecretpassword'"
conn = psycopg2.connect(conn_string)
cursor.execute(
""" 
select 
* 
from 
planet_osm_point limit 10
""")

result = cursor.fetchall()