0

so I am making an sql database for businesses to use and when I enter the date it doesn't work and it throws up this error:

Traceback (most recent call last):
  File "/Users/samwest/Desktop/python stuff/inflows database.py", line 18, in <module>
    c.execute("INSERT INTO outflows VALUES ('{}',{},{},{})".format(item, quantity, cost, date))
sqlite3.OperationalError: table outflows has 3 columns but 4 values were supplied

in the past also when I have got the input working instead of displaying the date it just then takes away each number in the date from each other. There is also the issue that the name of the item doesn't show when the {item} is used. here is the code that I am trying to get working now:

item = input("please enter the name of an item: ")
quantity = int(input("enter quantity of {item}s: "))
cost = int(input("enter the cost of one {item}: "))
date = (input("enter the date at which the {} was purchased(formtat YYYY-MM-DD): "))
c.execute("INSERT INTO proto3 VALUES ('{}',{},{},{})".format(item, quantity, cost, date))

any help would be great thanks

  • 1
    Apart of the issue identified in the answer, don't use string formatting to inject values in SQL statements. Use parameter substitution, as described in the answers to [this question](https://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python) – snakecharmerb Jan 22 '22 at 16:18

1 Answers1

0

you are casting the dateinput to int, which does not work. When you are putting in '2022-10-02' this is string and cannot be casted to int. You have to remove the int() cast. Apart from that, you probably have to add quotes around the date too. So the line should read like

date = input("enter the date at which the {} was purchased(formtat YYYY-MM-DD): ")
c.execute("INSERT INTO proto3 VALUES ('{}',{},{},'{}')".format(item, quantity, cost, date))

Simon Hawe
  • 3,968
  • 6
  • 14
  • I have tried this it now throws up a new error that says c.execute("INSERT INTO outflows VALUES ('{}',{},{},{})".format(item, quantity, cost, date)) sqlite3.OperationalError: table outflows has 3 columns but 4 values were supplied – samuel west Jan 22 '22 at 16:17