如何在sqllite3数据库中插入多行



我试图选择行并从DB表中提取它们,然后将它们插入到列表中,这样我就可以一次将所有行插入数据库,但我遇到了一个错误。

def paid_or_returned_buyinchecks(self(:

date = datetime.now()
now = date.strftime('%Y-%m-%d')
self.tenlistchecks=[]
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.dashboard_buying_checks_dates = self.cursorObj.execute("select id, paymentdate , paymentvalue, car ,sellername from cars_buying_checks where nexttendays=?",(now,))
self.dashboard_buying_checks_dates_output = self.cursorObj.fetchall()
self.tenlistchecks.append(self.dashboard_buying_checks_dates_output)
print(self.tenlistchecks)

self.dashboard_buying_checks_dates = self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)",[self.tenlistchecks])
self.con.commit()

但我犯了一个错误:[[(120,'21-08-2022','1112','阿尔法·罗密欧','james'(,(122,'21-08.2022','465','别克','daniel'(self.dashboard_buying_checks_dates=self.cursorObj.executemany(sqlite3.ProgrammingError:提供的绑定数不正确。当前语句使用5,并且提供了1。

self.cursorObj.fetchall()返回元组列表,这是您需要提供给executemany的内容,因此

self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)",self.tenlistchecks)

不是

self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)",[self.tenlistchecks])

最新更新