gpt4 book ai didi

python - 如何调试 cursor.execute (psycopg2) is NoneType

转载 作者:行者123 更新时间:2023-11-29 12:44:28 28 4
gpt4 key购买 nike

我尝试对我的 postgres 数据库运行 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
""")

结果是 Nonetype,所以一定是有问题吗?

我做错了什么?我该如何调试它?

最佳答案

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

关于python - 如何调试 cursor.execute (psycopg2) is NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32279048/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com