【发布时间】:2016-09-19 11:35:40
【问题描述】:
我正在尝试向 MySQL 表中添加多个值,代码如下:
Try:
cursor.execute("INSERT INTO companies_and_charges_tmp (etags, company_id, created, delivered, satisfied, status, description, persons_entitled) VALUES ('%s, %s, %s, %s, %s, %s, %s, %s')" % (item['etag'], ch_no, item['created_on'], item['delivered_on'], item['satisfied_on'], item['status'], item['particulars'][0]['description'], item['persons_entitled'][0]['name']))
Except KeyError:
pass
问题是这段代码在循环中,有时正在插入的值之一会丢失,这将导致 Key Error 取消整个插入。
如何通过 KeyError,所以当与正在插入的项目之一相关的 KeyError 发生时,其他项目仍会添加到表中,而缺少的项目只是保留为 NULL?
【问题讨论】:
标签: python mysql sql-insert mysql-python