任何人都可以帮助我解决此问题吗?这是我的代码为何无法检测到该列? sqlite3.OperationalError:无此类列:无
import sqlite3
import tkinter
from tkinter import *
from tkinter import ttk
def Daftar():
window = Tk()
window.title("Welcome to TutorialsPoint")
window.geometry('400x400')
window.configure(background = "grey");
Lnpm = Label(window, text="Please Input Your npm: ").grid(row=0, column=0)
Lnama = Label(window,text="Please Input Your nama: ").grid(row=1, column=0)
Ljurusan = Label(window,text="Please Input Your jurusan: ").grid(row=2, column=0)
npm1 = Entry(window).grid(row = 0,column = 1)
nama1 = Entry(window).grid(row = 1,column = 1)
jurusan1 = Entry(window).grid(row = 2,column = 1)
def Clicked():
npm = npm1
nama = nama1
jurusan = jurusan1
connect = sqlite3.connect('C:///Users///Marvin///Desktop///FaceRecognition-Presernsi-sql///SQL///Presensi.db')
cur = connect.cursor()
connect.execute("INSERT OR IGNORE INTO user(npm,nama,jurusan) Values("+str(npm)+",' "+str(nama)+" ',' "+str(jurusan)+" ' )")
connect.execute("INSERT OR IGNORE INTO presensi(nama) Values(' "+str(nama)+" ')")
connect.commit()
cur.close()
tn = ttk.Button(window ,text="Register",command=Clicked).grid(row=3,column=0)
答案 0 :(得分:1)
检查表user
中实际上存在以下几列:npm, nama, jurusan
;并在表presensi
中:nama
。
connect.execute("INSERT OR IGNORE INTO user(npm,nama,jurusan) Values("+str(npm)+",' "+str(nama)+" ',' "+str(jurusan)+" ' )") connect.execute("INSERT OR IGNORE INTO presensi(nama) Values(' "+str(nama)+" ')")
如果您要更新这样的文本列,则必须用 quotes 括起来,即它应该是...values('" + str(npm) + "'"...
但是,像这样构建sql查询会导致sql注入攻击的可能性。最好使用parameterised queries代替,使用占位符:
cur.execute("INSERT OR IGNORE INTO user(npm,nama,jurusan) values(?,?,?)", (str(npm),str(nama),str(jurusan))
cur.execute("INSERT OR IGNORE INTO presensi(nama) values(?)", (str(nama),))