Python执行SQL更新查询时卡住

时间:2019-01-14 19:23:54

标签: python sql

我的python脚本卡在游标执行上,我很难找出原因。

db = cx_Oracle.connect('user/password@user')
cursor = db.cursor()

for i in range(1, 10):
    insert_data = """
    update products set prd_stock_holder = '~',prd_prod_quality = 'FREE' 
    where PRD_PRI_ID = (select prd_pri_id from products join PRODUCT_INFOS
        on prd_pri_id = pri_id
            where PRI_code = '%s')""" %i

    cursor.execute(insert_data) # stuck here

    print "product %s updated" %i # never printed

db.commit()

2 个答案:

答案 0 :(得分:1)

我在插入MySQL数据库时遇到了类似问题。 在循环内移动db.commit()解决了我的问题。

db = cx_Oracle.connect('user/password@user')
cursor = db.cursor()

for i in range(1, 10):
    insert_data = """
    update products set prd_stock_holder = '~',prd_prod_quality = 'FREE' 
    where PRD_PRI_ID = (select prd_pri_id from products join PRODUCT_INFOS
        on prd_pri_id = pri_id
            where PRI_code = '%s')""" %i

    cursor.execute(insert_data) # stuck here
    db.commit()
    print "product %s updated" %i # never printed

答案 1 :(得分:0)

一个原因可能是Oracle RDBMS锁定。您可以尝试检查会话是否被锁定以等待资源(例如http://www.dba-oracle.com/t_tracking_oracle_blocking_sessions.htm)。

请记住,锁可能已连接到数据库决定应用的执行计划。通过重写更新查询,您可能会受益于更薄的锁获取。