0

Hello I've been attempting to fix an issue with a python OperationalError in a sqlite3 database. I am using a question mark as a placeholder for an insert statement. But I am getting the following message.

db.execute("UPDATE accounts SET balance = ? WHERE (name = ?);", (new_balance, self.name))

db.execute("INSERT INTO history VALUES(?, ?, ?, ?);", (deposit_time, self.name, amount, picked_zone))
sqlite3.OperationalError: table history has 3 columns but 4 values were supplied.

I looked over the code and tried removing one of the items in the brackets to the right. I get the same message.

I verified the syntax of an insert statement. I've looked up some solutions that I have tried but they did not work with my version. Like the following.

sql = ''' INSERT INTO tasks(name,priority,status_id,project_id,begin_date,end_date)
              VALUES(?,?,?,?,?,?) '''

I reversed the values parameter. It didn't work. I tried using triple quotes. That did not work. I'm not sure how to fix this.[enter image description here](https://i.stack.imgur.com/PEOfC.png)

bjornasm
  • 2,211
  • 7
  • 37
  • 62

1 Answers1

0

It seems like your history table only have 3 columns, but you try to put four contents of data into it. I would have a look on the history table and its columns. You can check the columns of your table using sqlite3 in terminal or python by executing the command:

pragma table_info(history);

or

SELECT * FROM history limit 1;

Judging by the error message both of these commands will show that the history table have only three columns. To fix this you have to either re-create the history table so it has all the columns you need, or insert the appropriate amount of columns.

One way to make this more clear in your code is to specify the columns you are inserting into.

INSERT INTO history (column1,column2 ,..) (...)
bjornasm
  • 2,211
  • 7
  • 37
  • 62