[英]python sqlite3 executemany except for each row
我想执行 executemany 并为每一行获取异常或成功状态
data = [('H19150', 9677, 519.0, 558.0),#success
('345', 9666, 606.4, 651.8),# primary key exception
('H19159', 9665, 657.4, 705.0),#unique key exception
('H19215', 9678, 528.4, 569.4),#success
('6546', 324, 528.4, 569.4),#foreign key exception
('H19158', 45, 528.4, 569.4)]#success
import sqlite3
try: conn.executemany('INSERT or IGNORE INTO coated VALUES (?,?,?,?)', data)
except #catch error for each row:
#what to write here
我想为每一行得到错误像:
success
primary key exception
unique key exception
success
foreign key exception
success
在 executemany 结束后的一个变量中。
是否有可能做到这一点?
据我所知,如果您使用executemany()
,那么整个插入将表现为单个成功/失败事务。 如果你想获得关于每个元组的反馈,那么你应该迭代你的列表并为每个元组做一个单独的插入:
for tuple in data:
try: conn.execute('INSERT or IGNORE INTO coated VALUES (?)', tuple)
except #catch error for each row:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.