簡體   English   中英

Python SQLlite3執行命令未寫入值

[英]Python SQLlite3 execute command not writing values

當我嘗試運行程序時,出現錯誤

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

如注釋中所述,您的SQL語句中有一個錯字( Orders .. Orders4 )。
但是,實際的問題出在定義INSERT命令的字符串中(最后一行)。 如果要評估變量first_namelast_name等(即,其實際值以字符串結尾),則必須相應地格式化字符串。 當前,它們是簡單的字符串。

當數據庫執行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))

hth,丹尼斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM