如何检查Python中MySQLdb插入是否成功?

如何检查Python中MySQLdb插入是否成功?,第1张

如何检查Python中MySQLdb插入是否成功?

修改后,您的代码将不会提交(您的修改将回滚)。那就是你应该在下面添加以下行

cursor.execute

conn.commit()

插入失败,将引发

MySQLdb.IntegrityError
,因此您应该准备抓住它。

因此,您的代码应类似于:

sql_insert = """insert into new_files (videos_id, filename, is_processing)values (%s,%s,1)"""cursor = conn.cursor()try:    affected_count = cursor.execute(sql_insert, (id, filename))    conn.commit()    logging.warn("%d", affected_count)    logging.info("inserted values %d, %s", id, filename)except MySQLdb.IntegrityError:    logging.warn("failed to insert values %d, %s", id, filename)finally:   cursor.close()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5617440.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存