python으로 첫 번째 '업데이트'쿼리를 작성했습니다. 올바른 것으로 보이지만 출력 확인을 위해 출력을받는 방법을 잘 모르겠습니다 ..postgres 'update'쿼리가 파이썬에서 작동했는지 확인
이 은 두 번째에 그와 함께 첫 번째 열에 값을 CSV 파일을로드하고 대체 할 예정이다 :
def main():
try:
conn=psycopg2.connect("dbname='subs' user='subs' host='localhost' password=''")
except:
print "I am unable to connect to the database."
sys.exit()
with open("dne.txt", "r+") as f:
for line in f:
old = line.split(',')[0].strip()
new = line.split(',')[1].strip()
cur = conn.cursor()
cur.execute("UPDATE master_list SET subs = '{0}' WHERE subs = '{1}';".format(new, old))
conn.commit()
results = cur.fetchall()
for each in results:
print str(each)
if __name__=="__main__":
main()
내가 결과를 생각 튜플로 돌아올 것 (각 변경 UPDATE 1?) 하지만 대신 오류가 발생했습니다.
psycopg2.ProgrammingError: no results to fetch
내 쿼리가 작동하지 않고 업데이트가 없거나, 시도하고있는 것처럼 fetchall()을 사용할 수없는 경우 확실하지 않습니다.
의견이나 제안 환영합니다!
가져 오기 전에 데이터를 커밋 해 보셨습니까? 나는 cur.commit()을 의미합니다. 귀하의 케이스에서 유용할지 모르겠지만 때로는 커밋하지 않는 것이 문제입니다. – Lafexlos
@ Lafexlos - cur.commit() AttributeError : 'psycopg2._psycopg.cursor'객체에 'commit'속성이 없습니다. – some1
아 죄송합니다. conn.commit()을 의미했습니다. 오타입니다. – Lafexlos