繁体   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