python - 当数据库中存在行时抛出异常
问题描述
我想在数据库 df 中插入所有数据框。如果有重复的行。我想在另一个日志表中插入这一行的一些元素。请问有谁知道该怎么做?我试过下面的代码:我没有成功捕捉到错误
#When i created my database df i put an UNIQUE key(element1, element2) So in my dataframe if i have two rows where (element1 in row i = element1 row i+1) and (element2 in row i = element2 row i+1) it is considered as duplicated.
try:
df.to_sql(con=cnx, name='df', if_exists='append', index=False)
except mysql.connector.Error as err:
logs = [df[0], df[1], 'INSERT', 'FAIl', str(err)]
cursor.execute(
"INSERT IGNORE INTO logtable (id, element, action, message) VALUES (%s, %s, %s, %s)",
(logs))
解决方案
推荐阅读
- laravel-5 - 如何使用 Eloquent 计算每一列并显示
- swift - NSOutlineView,如何获取选中的单元格
- android - 生产中的 Firebase 电话身份验证错误
- reactjs - React js - 我希望自动完成应该使用 primaryText 和 SecondaryText 搜索值
- java - Spring Boot MSSQL Kerberos 身份验证
- java - java中方法隐藏概念中“隐藏”一词的含义是什么?
- c# - Unity-C# 实例化不起作用
- arrays - 在 Swift 中将实例变量的属性保存到 Struct 的静态变量数组中
- outlook - 如何在 Outlook 中获取标准帐户
- react-native - 当数据源来自json数组时反应本机选择器给出错误