【问题标题】:python sqlite3 executemany except for each rowpython sqlite3 executemany 除了每一行
【发布时间】:2020-06-07 03:32:25
【问题描述】:

我想执行 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 结束后在一个变量中。

可以这样做吗?

【问题讨论】:

    标签: python sqlite except


    【解决方案1】:

    据我所知,如果您使用executemany(),那么整个插入将表现为单个成功/失败事务。如果你想获得关于每个元组的反馈,那么你应该迭代你的列表并为每个元组做一个单独的插入:

    for tuple in data:
        try: conn.execute('INSERT or IGNORE INTO coated VALUES (?)', tuple)
    except #catch error for each row:
    

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2020-06-20
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多