-1

I am trying to select rows and fetch them from the DB table and then insert them into a list so I can insert all of the rows at once into the database, but I got an error.

def paid_or_returned_buyingchecks(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()

but I got an error : [[(120, '21-08-2022', '1112', 'Alfa Romeo', 'james'), (122, '21-08-2022', '465', 'Buick', 'daniel '), (123, '21-08-2022', '789', 'Buick', 'daniel ')]] self.dashboard_buying_checks_dates = self.cursorObj.executemany( sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 5, and there are 1 supplied.

  • Print self.tenlistchecks before inserting. – PChemGuy Aug 31 '22 at 03:57
  • @PChemGuy thank you for the answering, this is exactly what I've got when printing (self.tenlistchecks) : [[(120, '21-08-2022', '1112', 'Alfa Romeo', 'james'), (122, '21-08-2022', '465', 'Buick', 'daniel '), (123, '21-08-2022', '789', 'Buick', 'daniel ')]] – asaad kittaneh Aug 31 '22 at 18:24

1 Answers1

0

self.cursorObj.fetchall() returns a list of tuples, which is what you need to feed to executemany, so

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

not

self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)",[self.tenlistchecks])
PChemGuy
  • 1,582
  • 3
  • 6
  • 18
  • I've tried this and I got: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 5, and there are 2 supplied. to clarify the situation I have 2 rows only in the table – asaad kittaneh Sep 01 '22 at 02:32
  • Run this https://trinket.io/python3/6d00f7098e, then change the data to yours. If you still have issues, provide the link to your fiddle showing the issue. – PChemGuy Sep 01 '22 at 05:49
  • @ PChemGuy I got the same error – asaad kittaneh Sep 01 '22 at 15:13
  • Where is the link to your fiddle? If you want my help, do the same thing. I want a link on the same site with your code and and data showing the error. – PChemGuy Sep 01 '22 at 15:23