Search code examples
pythonsqlsql-delete

Cannot DELETE data in Access by Python


Cannot delete data by python. I use Entry to delete. Maybe Entry it is not used in deleting, so what is a problem?

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


Solution

  • Just write a tuple or row. To ensure it is a tuple use a comma:

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