我想执行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: