简体   繁体   English

python sqlite3 除每一行外执行许多

[英]python sqlite3 executemany except for each row

I want to execute executemany and get exception or suceeded status 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 

I want to get error for each row Something like:我想为每一行得到错误像:

success
primary key exception
unique key exception
success
foreign key exception
success

in one variable after executemany is over.在 executemany 结束后的一个变量中。

Is it possible to do this?是否有可能做到这一点?

As far as I know, if you use executemany() , then the entire insert will behave as a single succeed/fail transaction.据我所知,如果您使用executemany() ,那么整个插入将表现为单个成功/失败事务。 If you want to get feedback on each tuple, then you should iterate your list and do a separate insert for each tuple:如果你想获得关于每个元组的反馈,那么你应该迭代你的列表并为每个元组做一个单独的插入:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM