0

When I try to run the program, it comes up with the error

Traceback (most recent call last):
  File "G:\documents\GCSE\Computing\Python Files\Databases\Database 1.py", line 15, in <module>
cursor.execute('''INSERT INTO Orders VALUES (first_name,last_name,number,date,order_value)''')
sqlite3.OperationalError: no such column: first_name

The code is as follows, any help that could be given would be appreciated ;D

cursor.execute('''CREATE TABLE Orders4 (customer_first_name text,customer_second_name text,order_number text,order_date date,order_contents text)''')
first_name = input("What is the customer's first name: ")
last_name = input("What is the customer's last name: ")
number = input("What is the order number: ")
order_value = input("What is the order: ")
date = time.strftime("%d/%m/%Y")
cursor.execute('''INSERT INTO Orders VALUES(first_name,last_name,number,date,order_value)''')
1
  • 1
    You call the table Orders4 when you create it, then try to reference it as Orders Commented Oct 20, 2016 at 12:50

1 Answer 1

1

As mentioned in the comments, there is a typo in your SQL statements (Orders vs. Orders4).
The actual problem, however, is in the string where you define your INSERT command (last line). If you want the variables first_name, last_name, etc to be evaluated (i.e. their actual values ending up in the string) you have to format the string accordingly. Currently, they are simple strings.

When the database executes the INSERT command it sees the word first_name and evaluates it as a reference to the column "first_name", which doesn't exist.

If you would quote the variables like so

cursor.execute('''INSERT INTO Orders VALUES("first_name","last_name","number","date","order_value")''')

the command would work, but you would end up with the actual words "first_name" in the row in your database.

Read about string formatting in the above link and try the following

cursor.execute('''INSERT INTO Orders VALUES({fn},{ln},{nr},{dt},{ov})'''.format(fn=first_name,ln=last_name,nr=number,dt=date,ov=over_value))

hth, Denis

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.