Deleting a row of data table

i created a database by follwing program in python… now i want to delete a row from this table(that is first name,lastname and id of one person and that is a label).what shoud i di? please help

from tkinter import *
import sqlite3

conn = sqlite3.connect('bmidatabase.db')
c = conn.cursor()

class App:
    def __init__(self,master):
##        frame = Frame(master)
##        frame.pack()
        self.master=master
        a= StringVar()
        b= StringVar()
        a1= StringVar()
        b1= StringVar()
        c= StringVar()

        self.button = Button(self.master,text="open", fg="red", command=self.ouvrir)
        self.button.pack(side=LEFT)

        self.button2 = Button(self.master,text="create", command=self.tabluh)
        self.button2.pack(side=LEFT)

        self.button3 = Button(self.master,text="close DB", command=self.fermer)
        self.button3.pack(side=LEFT)

        self.button31=Button(self.master,text="Exit",fg='red',command=self.exit)
        self.button31.pack(side=LEFT)

        self.button4 = Button(self.master,text="insert rec", command=self.insertar)
        self.button4.pack(side=LEFT)

        self.button5 = Button(self.master,text="list rec", command=self.listar)
        self.button5.pack(side=LEFT)

        self.a = Entry(self.master)
        self.a.pack(side=BOTTOM)

        self.b = Entry(self.master)
        self.b.pack(side=BOTTOM)

        self.c = Entry(self.master)
        self.c.pack(side=BOTTOM)
    def ouvrir(self):
        self.con=sqlite3.connect('maddb')
        self.cur=self.con.cursor()
    def tabluh(self):

        c.execute('''CREATE TABLE xxx(id INTEGER,firs stringvar(10),las stringvar(10))''')
    def fermer(self):
        self.con.close()
    def insertar(self):
        a1=self.a.get()
        b1=self.b.get()
        c1=int(self.c.get())
        c.execute("INSERT INTO xxx (id, firs,las ) VALUES (?, ?, ?)",(c1, a1, b1))
        conn.commit()
    def listar(self):
        c.execute('SELECT * FROM xxx')
        print(c.fetchall())
    def exit(self):
        #Exit protocol for the exit button. This part is completely done.#
          self.master.destroy()
root = Tk()
root.title("Dbase")
app=App(root)
root.mainloop()

Just execute the corresponding SQLite command : DELETE FROM (table name) WHERE (column name) = (or LIKE) (column value for the row you wish to delete)

First create a new button:

self.button6 = Button(self.master, text="del rec", command=self.delete)
self.button6.pack(side=LEFT)

Now create a new function named delete:

def delete(self):
    id = int(self.c.get())
    c.execute("DELETE FROM xxx WHERE id=?", id)
    conn.commit()

i want that the user can delete a row by pushing a button…i dont want to delete it myself in my code befor been selected by the user…how can tkinter window understand which row has been selected???
please help…thank you

You need to put the columns and rows inside something like a multilistbox from TKTreectrl and make it so the primary key is the index of the row (zero indexing).

import TkTreectrl as treectrl

def Select(selected):
   print ('Selected items:', selected)

mlb = treectrl. MultiListbox(root)
mlb.pack(side= 'top' , fill= 'both' , expand= 1 )
mlb.focus_set()
mlb.configure(selectcmd=Select, selectmode='extended')
mlb.config(columns=( 'Column 1' , 'Column 2', ...etc ))
for row in c.fetchall():
        mlb.insert( 'end' ,*map(unicode,row))

thank you very much!
but i got an error
when i entered an id (that had been saved before) and then pushed “delete” button, i faced with this error “prameters are of unsupported type” in line “c.execute(“DELETE FROM xxx WHERE id=?”, id)”
what is the problem?:cry:

Try this

def delete(self):
    id = int(self.c.get())
    c.execute("DELETE FROM xxx WHERE id=?", (id,))
    conn.commit()

And make sure the id is an integer type.

2 Likes

thank you…LIKE:smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.