gpt4 book ai didi

python - 在重复输入时退出Python MySQLdb

转载 作者:行者123 更新时间:2023-12-03 07:59:44 29 4
gpt4 key购买 nike

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

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

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)

先感谢您。

最佳答案

import sys
...
...
...

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

关于python - 在重复输入时退出Python MySQLdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30913277/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com