2016-10-20 28 views
0

当我尝试运行程序,它自带了错误Python的SQLlite3执行命令不写值

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 

的代码如下,即可以给予,将不胜感激任何帮助; 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

您调用表'Orders4'当你创建它,然后尝试引用它作为'Orders' – asongtoruin

回答

1

正如在评论中提到的,在你的SQL语句(订单Orders4)一个错字。
但是,实际的问题出现在您定义INSERT命令的字符串中(最后一行)。 如果您想要对变量first_name,last_name等进行评估(即它们的实际值以字符串结尾),您必须相应地为该字符串format。目前,它们是简单的字符串。

当数据库执行INSERT命令时,它会看到字first_name,并将其评估为对“first_name”列的引用,该列不存在。

如果您想引用变量,像这样

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

命令的工作,但你最终会在你的数据库中的行中的实际的话“FIRST_NAME”。

阅读串在上面的链接的格式,并尝试以下

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

心连心,丹尼斯