简体   繁体   English

cursort.execute/cursor.callproc 没有返回错误但没有执行任何操作

[英]cursort.execute/cursor.callproc returns no error but nothing is executed

When i run this SQL query via psql client it runs for several seconds (~90 seconds, that's normal since its a huge table) and it return, then i can check that my line is successfully inserted.当我通过 psql 客户端运行这个 SQL 查询时,它运行了几秒钟(~90 秒,这是正常的,因为它是一个巨大的表)然后它返回,然后我可以检查我的行是否已成功插入。

SELECT merge_data('898989', '111111111', '10000')

It is a stored procedure that runs an UPDATE or INSERT, the procedure is ran without errors and i get my entry in the table.它是一个运行 UPDATE 或 INSERT 的存储过程,该过程运行没有错误,我在表中得到了我的条目。

When trying to do the same from a python program, the query take 2 seconds and returns no errors, i get nothing in my table;尝试从 python 程序执行相同操作时,查询需要 2 秒并且没有返回任何错误,我的表中什么也没有; for information, the statement is successfully executed on postgresqk backed (i can see it in pgsql logs), here's my code snippet:有关信息,该语句在 postgresqk 支持上成功执行(我可以在 pgsql 日志中看到它),这是我的代码片段:

conn = psycopg2.connect("...")
cursor = conn.cursor()

try:
    cursor.callproc("merge_data", ['898989', '111111111', '10000'])
except:
    print "ERROR !"

cursor.close()

Resolved !解决 !

By default, psycopg2 doesnt autocommit on each executed statement, so once i close the connection (at the end) all pending transactions are discarded as if a ROLLBACK was performed.默认情况下,psycopg2 不会在每个执行的语句上自动提交,因此一旦我关闭连接(最后),所有挂起的事务都将被丢弃,就像执行了 ROLLBACK 一样。

One may call conn.commit() after each cursor.execute() or .callproc, or just set it at a global scope using可以在每个 cursor.execute() 或 .callproc 之后调用 conn.commit(),或者只是使用

conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

conn.commit() should be used instead changing the isolation level, unless you realy need the autocommit feature.除非您确实需要自动提交功能,否则应该使用 conn.commit() 来代替更改隔离级别。

Python DB2 API Python DB2 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM