![](/img/trans.png)
[英]Traceback (most recent call last): File “<stdin>”, line 1, in <module>
[英]can i escape this error Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “<stdin>”, line 2, in data_entry
我正在嘗試將一些信息放入數據庫
import sqlite3
conn = sqlite3.connect("tutorial.db")
c=conn.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS stuffPlot(unix REAL,datestamp Text,keyword TEXT,value REAL)")
def data_entry(x,y,z,w):
c.execute("INSERT INTO stuffPlot VALUES({},{},{},{})".format(x,y,z,w))
conn.commit()
c.close()
conn.close()
x=int(input("enter a number"))
y=str(input("enter a str"))
z=int(input("enter a number"))
w=str(input("enter a str"))
create_table()
data_entry(x,y,w,z)
我想寫入數據庫,但是會產生以下數據庫錯誤:
data_entry(x,y,w,z)追溯(最近一次調用為最新):文件“”,行1,在文件“”中,行2,在data_entry sqlite3.OperationalError:無此類列:name
您的字符串缺少引號,因此它們被視為列名,請嘗試以下操作:
c.execute("INSERT INTO stuffPlot VALUES({},'{}','{}',{})".format(x,y,z,w))
編輯
而不是上面的行(容易受到SQL注入的影響),您應該這樣做:
c.execute("INSERT INTO stuffPlot VALUES(?,?,?,?)", (x,y,z,w))
嘿,您的最后一行是這樣的:
data_entry(x,y,w,z)
它的x,y,w,z,但是在您的函數定義中,您收到的是:
def data_entry(x,y,z,w): #sequence is x,y,z,w
因此,對於什么是什么變量造成了一些困惑。 而且您的字符串也需要用@scope括在單引號內。
所以這是一個工作代碼
import sqlite3
conn = sqlite3.connect("tutorial.db")
c=conn.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS stuffPlot(unix REAL,datestamp Text,keyword TEXT,value REAL)")
def data_entry(x,y,z,w):
c.execute("INSERT INTO stuffPlot VALUES({},'{}',{},'{}');".format(x,y,z,w))
#fixed the quotes above
conn.commit()
c.close()
conn.close()
x=int(input("enter a number"))
y=str(input("enter a str"))
z=int(input("enter a number"))
w=str(input("enter a str"))
create_table()
data_entry(x,y,z,w) #fixed x,y,w,z to x,y,z,w
如果您有任何疑問,請發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.