SQL语句未插入

时间:2017-08-03 20:50:52

标签: python sql python-3.x tkinter

我试图将用户输入插入数据库。它应该工作,但我一直收到一个奇怪的错误信息。如果可以,请帮忙。

我的代码所做的是显示带有几个输入框的GUI。当用户单击“确定”按钮时,它将文本存储到变量中。然后我使用另一个函数将文本插入Access表。

下面的代码,错误信息在它下面。

conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\cqt7wny\Documents\new_UPS.accdb;'
conn = pyodbc.connect(conn_string)
cur = conn.cursor()


win = Tk()

win.title("UPS")
win.geometry("600x600")
win.configure(background='white')

fr = Frame(win)
fr.pack(side = BOTTOM)

listlab = []

def clear():
    for l in listlab:
        l.destroy()

jname = StringVar()
jstart = StringVar()
jend = StringVar()
jfields = StringVar()
jdeli = StringVar()

job_name = ''

def mhello():
    job_name = jname.get()
    job_start = jstart.get()
    job_end = jend.get()
    job_fields = jfields.get()
    delimiter = jdeli.get()
    Label(fr, text=job_name).grid(row=1,column=2)
    Label(fr, text=job_start).grid(row=2, column=2)
    Label(fr, text=job_end).grid(row=3, column=2)
    Label(fr, text=job_fields).grid(row=4, column=2)
    Label(fr, text=delimiter).grid(row=5, column=2)


    cur.execute("INSERT INTO set VALUES (%s,%s,%s,%s,%s)", (job_name,job_start,job_end,job_fields,delimiter))

    conn.commit()
def callback1():
    clear()
    w1 = Label(fr, text="Job Name : ")
    w1.grid(row=1, column=0)
    listlab.append(w1)
    w2 = Label(fr, text="Job Start : ")
    w2.grid(row=2, column=0)
    listlab.append(w2)
    w3 = Label(fr, text="Job End : ")
    w3.grid(row=3, column=0)
    listlab.append(w3)
    w4 = Label(fr, text="Fields : ")
    w4.grid(row=4, column=0)
    listlab.append(w4)
    w5 = Label(fr, text="Delimiter : ")
    w5.grid(row=5, column=0)
    listlab.append(w5)

    e1 = Entry(fr, textvariable=jname)
    e1.grid(row=1, column=1)
    listlab.append(e1)
    e2 = Entry(fr, textvariable=jstart)
    e2.grid(row=2, column=1)
    listlab.append(e2)
    e3 = Entry(fr, textvariable=jend)
    e3.grid(row=3, column=1)
    listlab.append(e3)
    e4 = Entry(fr, textvariable=jfields)
    e4.grid(row=4, column=1)
    listlab.append(e4)
    e5 = Entry(fr, textvariable=jdeli)
    e5.grid(row=5, column=1)
    listlab.append(e5)
    mbutton = Button(fr, text="OK", command=mhello)
    mbutton.grid(row=5, column=3)



def callback2():
    clear()
    w2 = Label(fr, text="2")
    w2.pack()
    listlab.append(w2)




b1 = Button(win,text="Set Up", command=callback1)
photo1 = PhotoImage(file="setupp.gif")
b1.config(image=photo1,width="50",height="50")

b2 = Button(win,text="Run Data", command=callback2)
photo2 = PhotoImage(file="run.gif")
b2.config(image=photo2,width="50",height="50")

b1.place(relx=0.2, rely=0.2, anchor=CENTER)
b2.place(relx=0.4, rely=0.2, anchor=CENTER)

win.mainloop()

print(job_name)

错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\cqt7wny\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/cqt7wny/PycharmProjects/kk/classes.py", line 48, in mhello
    cur.execute("INSERT INTO set VALUES (%s,%s,%s,%s,%s)", (job_name,job_start,job_end,job_fields,delimiter))
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (-3502) (SQLPrepare)')

1 个答案:

答案 0 :(得分:1)

如果set是您的表的名称,它也是一个SQL保留字,您需要用方括号将其转义。

INSERT INTO [set] VALUES (%s,%s,%s,%s,%s)