【发布时间】:2015-01-11 19:45:21
【问题描述】:
我在 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)
无论我做什么,它总是返回我 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'
【问题讨论】:
标签: python psycopg2 amazon-redshift