python – 处理psycopg2中的错误 psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, comm
生活随笔
收集整理的這篇文章主要介紹了
python – 处理psycopg2中的错误 psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, comm
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題
import psycopg2 # 數據庫連接參數 conn = psycopg2.connect(database="xxx", user="xx", password="xxx", host="192.168.1.2", port="5432") cur = conn.cursor() cur.execute("SELECT * FROM test;") rows = cur.fetchall() # all rows in table print(rows) for i in rows:print(i) conn.commit() cur.close() conn.close()通過psycopg2,調用數據的時候報錯psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, comm
?
原因
暫不明確
?
解決方案
改為下面的代碼就正常訪問到了數據庫信息
import psycopg2try:connection = psycopg2.connect(user="xxx",password="xx@#29",host="127.0.0.1",port="5432",database="xxx")cursor = connection.cursor()postgreSQL_select_Query = "select * from xxx"cursor.execute(postgreSQL_select_Query)mobile_records = cursor.fetchall() for row in mobile_records:print("Id = ", row[0], )print("Model = ", row[1])print("Price = ", row[2], "\n")except (Exception, psycopg2.Error) as error :print ("Error while fetching data from PostgreSQL", error)finally:#closing database connection.if(connection):cursor.close()connection.close()print("PostgreSQL connection is closed")?
總結
以上是生活随笔為你收集整理的python – 处理psycopg2中的错误 psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, comm的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用python操作postgresql
- 下一篇: Windows dos 在某个目录下的所