python – 处理psycopg2中的错误 psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, comm
最新推荐文章于 2023-01-06 15:11:22 发布
studyer_domi
于 2020-10-21 19:53:38 发布
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 psycopg2
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
问题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 tableprint(rows)for i in
我们在PG数据库,关闭了事务的自动提交的情况下,会经常的遇到这样的问题
ERROR:
current
tran
sac
t
ion
is
abort
ed
,
com
mands ignor
ed
until end of
tran
sac
t
ion
block
为什么会造成了这样的问题,原因是
Postgres数据库
中
,同一事务
中
如果某次数据库操作
中
出错的话,那这个事务以后的数据库都会出错。
我们举个很简单的例子
test=# select * from test1;
ERROR: relat
ion
"tes
错误
出现:
含有
错误
的查询后,选
中
insert语句无法执行,报错
current
tran
sac
t
ion
is
abort
ed
,
com
mands ignor
ed
until end of
tran
sac
t
ion
block
事务
中
含有
错误
,再执行DML时,事务无法正常进行。
我们在PG数据库,关闭了事务的自动提交的情况下,会经常的遇到这样的问题
ERROR:
current
tran
sac
t
ion
is
abort
ed
,
com
mands ignor
ed
until end of tr...
self.cur.execute(
sql
,Json(dict(item)))
builtins.TypeError: ‘Json’ object does not support indexing
这个函数的第二个参数是参数列表,是[],而JSON不能索引化,就是指不能转化成列表
self.cur.execute(
sql
,json.dumps(dict(item)))
builtins.TypeError: not all arguments convert
ed
during string f
psycopg2
in
failed
sql
tran
sac
t
ion
:
current
tran
sac
t
ion
is
abort
ed
com
mands ignor
ed
until end of
tran
sac
t
ion
block
当前事务被
中
止,直到事务结束前的命令都将被忽略。
可能是讲execute语句的内容当做的,具体的解决办法在cursor.execute()执行一次conn.
com
mit()。
一头雾水,其他的
sql
一般都是
//if (QMessageBox::Yes == QMessageBox:: quest
ion
(this,tr("Quest
ion
"),tr("are you ok?"),QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes))
SQL
报错:ERROR:
current
tran
sac
t
ion
is
abort
ed
,
com
mands ignor
ed
until end of
tran
sac
t
ion
block
1、报错快照:
Cause:
com
.kingbase8.util.K
SQL
Except
ion
: ERROR:
current
tran
sac
t
ion
is
abort
ed
,
com
mands ignor
ed
until end of
tran
sac
t
ion
block
2、原因:
会话
中
只要有一个
sql
执行报错了,这个