![](/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.