python pyscopg2 无法 select 来自 AWS redshift 的数据
python pyscopg2 unable to select data from AWS redshift
我在 AWS redshift 上有一个 postgres 数据库。目前我使用 Python Pyscopg2 与数据库交互。我发现我可以 运行:
curosr.execute("INSERT INTO datatype VALUES (%s, %s)", ("humidity", "some description"))
connect.commit()
但当我这样做时:
for row in cursor.execute("SELECT * FROM datatype"):
print(row)
无论我做什么,它总是 returns 我 None 打字。 任何人都可以给我建议与 redshift postgres 交互的正确方法是什么?
谢谢
根据需要,这里是完整代码
##encoding=utf8
from __future__ import print_function
import psycopg2
def connect():
conn = psycopg2.connect(host = "wbh1.cqdmrqjbi4nz.us-east-1.redshift.amazonaws.com",
port = 5439,
dbname = "dbname",
user = "user",
password = "password")
c = conn.cursor()
return conn, c
conn, c = connect()
c.execute("INSERT INTO table netatmo VALUES (%s, %s)", (1, 10.5))
conn.commit() # this works, and I can see the data in other db client software
for row in c.execute("SELECT * FROM netatmo").fetchall(): # this not working
print(row) # AttributeError: 'NoneType' object has no attribute 'fetchall'
你错过了 "fetchall()",
更新时 - 你不需要它,但在选择时 - 你必须获取结果
http://initd.org/psycopg/docs/cursor.html
您的代码应如下所示:
cursor.execute("SELECT * FROM datatype;")
for row in cursor.fetchall():
print(row)
我在 AWS redshift 上有一个 postgres 数据库。目前我使用 Python Pyscopg2 与数据库交互。我发现我可以 运行:
curosr.execute("INSERT INTO datatype VALUES (%s, %s)", ("humidity", "some description"))
connect.commit()
但当我这样做时:
for row in cursor.execute("SELECT * FROM datatype"):
print(row)
无论我做什么,它总是 returns 我 None 打字。 任何人都可以给我建议与 redshift postgres 交互的正确方法是什么?
谢谢
根据需要,这里是完整代码
##encoding=utf8
from __future__ import print_function
import psycopg2
def connect():
conn = psycopg2.connect(host = "wbh1.cqdmrqjbi4nz.us-east-1.redshift.amazonaws.com",
port = 5439,
dbname = "dbname",
user = "user",
password = "password")
c = conn.cursor()
return conn, c
conn, c = connect()
c.execute("INSERT INTO table netatmo VALUES (%s, %s)", (1, 10.5))
conn.commit() # this works, and I can see the data in other db client software
for row in c.execute("SELECT * FROM netatmo").fetchall(): # this not working
print(row) # AttributeError: 'NoneType' object has no attribute 'fetchall'
你错过了 "fetchall()", 更新时 - 你不需要它,但在选择时 - 你必须获取结果 http://initd.org/psycopg/docs/cursor.html
您的代码应如下所示:
cursor.execute("SELECT * FROM datatype;")
for row in cursor.fetchall():
print(row)