【问题标题】:Cannot UPDATE data in Python无法在 Python 中更新数据
【发布时间】:2018-02-05 15:52:53
【问题描述】:

无法在 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')

【问题讨论】:

    标签: python sql ms-access sql-update


    【解决方案1】:

    使用参数标记:

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

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 2021-10-02
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多