插入SQlite数据库时列名错误

时间:2019-03-16 14:27:06

标签: sqlite

def insertorupdate(ID,naam):
count=0
conn=sqlite3.connect("Faceset.db")
cmd="SELECT Id from students"
cursor=conn.execute(cmd)
for row in cursor:
    count=1
if(count==1):
    print("1")
    cmd="UPDATE students SET Name="+str(naam)+" WHERE Id="+str(ID)
else:
    print("2")
    cmd="INSERT INTO students(Id,Name) Values("+str(ID)+","+str(naam)+")"
conn.execute(cmd)
conn.commit()
conn.close()
ID=input('enter user ID')
naam=str(input("enter your name"))
insertorupdate(ID,naam)

每当我尝试运行此程序时,都会弹出以下错误。(输入为1和Ahaan)。该数据库具有两列Id和Name。

enter user ID1
enter your nameAhaan
2
Traceback (most recent call last):
File "capture.py", line 26, in <module>
insertorupdate(ID,naam)
File "capture.py", line 21, in insertorupdate
conn.execute(cmd)
sqlite3.OperationalError: no such column: Ahaan

1 个答案:

答案 0 :(得分:0)

您需要将名称括在单引号内

cmd = "UPDATE students SET Name = '" + str(naam) + "' WHERE Id = " + str(ID)

所以现在结果是:

UPDATE students SET Name = 'Ahaan' WHERE Id = 1

您的代码产生了以下内容:

UPDATE students SET Name = Ahaan WHERE Id = 1

因此Ahaan被用作列名。

了解更多here