Python SQLlite3执行命令不写值

时间:2016-10-20 12:23:35

标签: python sqlite python-3.x

当我尝试运行程序时,会出现错误

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 个答案:

答案 0 :(得分:1)

如评论中所述,您的SQL语句中存在拼写错误(订单订单4 )。
但是,实际问题在于您定义INSERT命令的字符串(最后一行)。 如果您希望评估变量first_namelast_name等(即它们的实际值以字符串结尾),则必须相应地format字符串。目前,它们是简单的字符串。

当数据库执行INSERT命令时,它会看到单词first_name,并将其评估为对#34; first_name&#34;列的引用,该列不存在。< / p>

如果你引用像这样的变量

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

命令会起作用,但你最终会得到实际的单词&#34; first_name&#34;在数据库的行中。

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

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))
丹尼斯,