如何调试光标.执行(psycopg2)是不典型的

2024-04-19 15:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着对我的postgres数据库运行SQL

我通过的连接对象

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

似乎没问题

^{pr2}$

结果是非类型的,所以一定是出了什么问题?在

我做错了什么?我如何调试这个?在


Tags: 对象import数据库localhosthostsqlstringport
1条回答
网友
1楼 · 发布于 2024-04-19 15:10:50

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()

相关问题 更多 >