无法在Python中更新数据

时间:2018-02-05 15:52:53

标签: python sql ms-access sql-update

无法在Python中更新数据。我使用Access作为数据库。什么是错误?

from tkinter import *
import pypyodbc
import ctypes

#Create connection
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/HP/Desktop/PITL;DBQ=C:/Users/HP/Desktop/PITL/PITL.mdb;')
cursor = con.cursor ()

form=Tk ()
form.title ("Add data")
form.geometry ('400x200')

a = Entry (form, width=20, font="Arial 16")
a.pack ()
b = Entry (form, width=20, font="Arial 16")
b.pack ()

def Add ():
    cursor.execute ("UPDATE Laws SET Law_name = a, Fine = b", (a.get(), b.get()))
    con.commit ()
    cursor.close ()
    con.close ()


Button=Button(form, text = 'PUSH ME', command = Add)
Button.pack ()

form.mainloop ()

错误是

Exception in Tkinter callback Traceback (most recent call last):   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)   File "C:/Users/HP/Desktop/PITL/UPDATE.py", line 19, in Add
    cursor.execute ("UPDATE Laws SET Law_name = a, Fine = b", (a.get(), b.get()))   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1491, in execute
    self._BindParams(param_types)   File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1284, in _BindParams
    raise ProgrammingError('HY000',error_desc) pypyodbc.ProgrammingError: ('HY000', 'The SQL contains 0 parameter markers, but 2 parameters were supplied')

1 个答案:

答案 0 :(得分:3)

使用参数标记:

cursor.execute ("UPDATE Laws SET Law_name = ?, Fine = ?", (a.get(), b.get()))