簡體   English   中英

我可以逃避此錯誤回溯(最近一次通話是最近一次):“文件” <stdin> ”,第1行,在 <module> 檔案“ <stdin> ”,第2行,在data_entry中

[英]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.

相關問題 追溯(最近一次通話):“文件” <stdin> ”,第1行,在 <module> Traceback(最近一次調用最后一次):文件“<stdin> ”,第 1 行,在<module> ModuleNotFoundError:沒有名為“Webhook”的模塊</module></stdin> 回溯(最近調用最后):文件“<stdin> ",第 1 行,在<module> NameError: 名稱 'db' 未定義</module></stdin> 獲取回溯(最近一次調用最后一次):文件“<stdin> &quot;,第 1 行,在<module> NameError:名稱“文件名”未定義 回溯(最近調用最后):文件“<stdin> “,第 1 行,在<module> NameError:名稱“游泳”未定義</module></stdin> Traceback(最近一次通話最后一次):文件“<stdin> &quot;,第 1 行,在<module> TypeError: object() 沒有參數 無法在python 3Traceback(最近一次調用為最新)中找出此錯誤:“文件” <stdin> ”,第1行,在 <module> NameError:未定義名稱“ xlrd” 為什么“回溯(最近一次通話最后一次):文件”<stdin> ”,第 1 行,在<module> ImportError:當我嘗試安裝 django 時,沒有名為 django 的模塊?</module></stdin> 我如何擺脫這個錯誤:Traceback(最近一次調用最后一次):文件“<string> ",第 1 行,在<module>文件“C:\程序?</module></string> 我收到此錯誤:Traceback(最近調用最后一次):文件“./prog.py”,第 4 行,在<module>關鍵錯誤:“價格”</module>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM