python - 无法在 Access by Python 中删除数据

标签 python sql sql-delete

无法通过python删除数据。我使用 Entry 来删除。也许 Entry 没有在删除时使用,那么有什么问题吗?

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')

lab_1=Label(form, text="What do you want to delete?")
lab_1.pack ()

en_1=Entry(form, width=20,bd=5)
en_1.pack()


def Add ():
    cursor.execute ("DELETE FROM Laws WHERE Law_name = ?", (en_1.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\DELETE.py", line 21, in Add cursor.execute ("DELETE FROM Laws WHERE Law_name = ?", (en_1.get())) File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc-1.3.4-py3.6.egg\pypyodbc.py", line 1475, in execute raise TypeError("Params must be in a list, tuple, or Row") TypeError: Params must be in a list, tuple, or Row

最佳答案

只需写入一个元组或行。为了确保它是一个元组,请使用逗号:

cursor.execute ("DELETE FROM Laws WHERE Law_name = ?", 
                (en_1.get(), )) #<- this comma

关于python - 无法在 Access by Python 中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666092/

相关文章:

python - LSTM 模型不会预测高于特定值的值(始终不相同的值)

python - 使用 Python 打开 .bat 文件 - 不起作用

sql - Postgresql 对 select 字段/表的访问限制

python - 将字符串转换为纪元时间戳

python - Heroku App 立即崩溃并出现 R10 和 H10 错误

mysql - 如何解决此问题 - 无法删除或更新父行 : a foreign key constraint fails

Java Mysql删除日期之间的记录语法错误

SQL Server - 使用 SQL 删除日期范围之间的行。日期转换失败

sql - 如何在 X 时间后删除一些记录或文件 - Laravel 5

sql - TOAD 脚本中的变量