我在将Oracle数据库使用的pyodbc遇到问题。我可以选择结果返回的语句,但是当我尝试更新记录时,该程序似乎在“ conn.execute(query)”行中挂起,没有错误消息。我必须强行退出该应用程序。
运行select语句检索预期结果。更新相同记录将挂起。 我还在其他应用程序上使用了此定义,而在选择和更新正常工作的MSSQL DB上没有问题。我还检查了用户/通行证是否具有读写权限
import pyodbc
def connect_odbc(conn_str, query, commit=False):
"""
Returns result set from connection string.
If commit is set to true, it will write to DB as well
"""
if commit:
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
cursor.execute(query) ### <--- Program hangs here
cursor.commit()
cursor.close()
return True
else:
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results
## Main Program ##
query = "select * from multishipdtl where cartonid = '100000000060506';"
queryupdate = "update multishipdtl set trackid='88888888' where cartonid = '100000000060506';"
conn_str = '''DSN=syntest;UID=userid;PWD=userpass'''
results = connect_odbc(conn_str, query) ### This connects ok with expected results
connect_odbc(conn_str, queryupdate, commit=True) ### Update statement hangs
答案 0 :(得分:0)
如果程序在执行cursor.execute("UPDATE ...")
时挂起,我首先要检查的是它是否在等待数据库响应:How do you show SQL executing on an Oracle database?
V$SESSION
's status
and wait_time
, and V$SESSION_WAIT
's event
。您可能还想尝试通过从SQL客户端(没有Python)运行UPDATE来重现此问题。ODBC trace log可能也有帮助。