pypyodbc在重试连接时不会抛出预期的异常

时间:2013-11-05 10:21:02

标签: python sql sql-server exception

我正在尝试使用pypyodbc将一些数据行插入到SQL Server数据库中。我编写的代码捕获任何pypyodbc.Error,如果发生这种情况,重试DB操作最多3次。在我的测试中,我设法尝试插入重复的密钥行数据 - 捕获异常并重试开始。问题是在重试时没有捕获异常并且代码挂起。这是代码:

# attempt to connect to the db - if unsuccessful, try up to 3 times
for i in range (3):
    try:
        # connect to db
        db = pypyodbc.connect("DRIVER={SQL Server};SERVER=SERVER;DATABASE=DB;UID=user;PWD=password")
        cursor = db.cursor()

        # Loop round assigning the values in the lists at the given index to a new variable
        for j in range (0,sector):
            value1 = list1[j]
            value2 = list2[j]
            value3 = list3[j]

            # print each of the values, as a sanity check
            print('Value 1 = ' + value1)
            print('Value 2 = ' + value2)
            print('Value 3 = ' + value3+ "\n")

            #if (value1 is not None and value2 is not None and value3 is not None):
            cursor.execute('''INSERT INTO table(field1,field2,field3) VALUES (?,?,?)''', (value1,value2,value3))
            cursor.commit()

        # close off the open connections
        cursor.close()
        db.close()
        # if successful, break out of the loop so there is no retry
        break

    # if there is a mysql exception thrown, print a message and retry
    except pypyodbc.Error, e:
        message = "Connection attempt " + str(i+1) + ": " + str(e)
        print(message)

    # if we reach this stage for a 3rd time, exit the program as no connection is possible and send an error email
    if i == 2:
        message = message + "\nThe script will now terminate."
        print (message)
        send_error_email(message)
        sys.exit()

第一次尝试时,值被写入控制台并且异常(u'23000',u“[23000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]无法在对象中插入重复的键行。报告。重试开始,值再次写入控制台。这是程序挂起的地方。为什么异常没有被重试?

1 个答案:

答案 0 :(得分:0)

您的代码每次失败时都会重复整个插入过程,失败后第一次尝试将再次失败,因为已经插入了记录。

我建议使用计数器代替for循环来限制你的试验:

trials = 0
db = pypyodbc.connect("DRIVER={SQL Server};SERVER=SERVER;DATABASE=DB;UID=user;PWD=password")
cursor = db.cursor()

for j in range (0,sector):
    assert trials < 3
    value1 = list1[j]
    value2 = list2[j]
    value3 = list3[j]
    print('Value 1 = {0}\nValue 2 = {1}\nValue 3 = {2}'.format(value1,value2,value3))
    try:
        cursor.execute('''INSERT INTO table(field1,field2,field3) VALUES (?,?,?)'''
                       , (value1,value2,value3))
        cursor.commit()
    except pypyodbc.Error, e:
        trials += 1
        message = "Connection attempt " + str(trials) + ": " + str(e)
        print(message)
    except AssertionError:
        message = message + "\nThe script will now terminate."
        print (message)
        send_error_email(message)
        sys.exit()

cursor.close()
db.close()

希望有所帮助。