Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :
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.

What I have tried:

Python
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()


when printing (self.tenlistchecks) thats what i got:
[[(120, '21-08-2022', '1112', 'Alfa Romeo', 'james'), (122, '21-08-2022', '465', 'Buick', 'daniel '), (123, '21-08-2022', '789', 'Buick', 'daniel ')]]
Posted
Updated 31-Aug-22 22:55pm
v2

1 solution

Your self.tenlistchecks contains a single element which is the array of tuples (double brackets):
Python
[[(120, '21-08-2022', '1112', 'Alfa Romeo', 'james'), (122, '21-08-2022', '465', 'Buick', 'daniel '), (123, '21-08-2022', '789', 'Buick', 'daniel ')]]
^^

It should be:
Python
[ (120, '21-08-2022', '1112', 'Alfa Romeo', 'james'), (122, '21-08-2022', '465', 'Buick', 'daniel '), (123, '21-08-2022', '789', 'Buick', 'daniel ')]


[EDIT]
Just as clarification it would probably be better to code it this way:
Python
self.tenlistchecks = None

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.tenlistchecks = self.cursorObj.fetchall()

Also, I am not sure why you are using the self. prefix on variables that look to be local to this function, rather than members of the class.
[/EDIT]
 
Share this answer
 
v2
Comments
asaad kittaneh 1-Sep-22 10:12am    
@Richard MacCutchan Thanks you for answering, is there a way to convert the double brackets to single bracket?
Richard MacCutchan 1-Sep-22 10:18am    
Yes, and my apologies for missing it earlier. Just remove the square brackets from the line:
    self.dashboard_buying_checks_dates = self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)",[self.tenlistchecks])

so it is
    self.dashboard_buying_checks_dates = self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)", self.tenlistchecks)
Richard MacCutchan 1-Sep-22 11:10am    
Actually on looking closer it may still be incorrect, as fetchall returns a list of the tuples. If it still fails try:
self.dashboard_buying_checks_dates = self.cursorObj.executemany("insert into paid_buying_checks VALUES(?,?,?,?,?)", self.tenlistchecks[0]) # insert the tuples inside the returned list
asaad kittaneh 1-Sep-22 10:59am    
@Richard MacCutchan I have tried this and I got :
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 5, and there are 1 supplied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900