The problem
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()Copy the code
An error when data through psycopg2, call psycopg2. Errors. InFailedSqlTransaction: the current transaction is aborted, comm
why
Temporarily not clear
The solution
Change to the following code to access the database information normally
import psycopg2 try: 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")Copy the code