Python MySQLdb在重复条目上退出

时间:2015-06-18 10:49:18

标签: python error-handling exception-handling mysql-python

我想在重复输入时退出程序这是我没有成功的事情:

我想处理错误,但不知道如何找到有关它的信息。

def connection():

    global servername, username, password, dbname

    con = mdb.connect(servername, username, password, dbname)

    return con;


def insert_vulnerabilities (CVE,Description,P_Date,U_Date,Score,Type):



    con = connection()



    with con:

        cur = con.cursor()


        try:
            cur.execute("INSERT INTO Vulnerabilities_test(CVE,Description,P_Date,U_Date,Score,Type) VALUES(%s,%s,%s,%s,%s,%s)",(CVE,Description,P_Date,U_Date,Score,Type))
            con.commit()
        except:

            sys.exit(0)

提前谢谢。

1 个答案:

答案 0 :(得分:0)

import sys
...
...
...

    try:
        write_cursor.execute(write_operation)
        db_writer.commit()
    except IntegrityError as ie:
        print("Duplicate key found. Exiting.")
        sys.exit(1)