0
I’m trying to crud sqlite3 with a GUI through Tkinter my table was created in DB Browser for sqlite. I’ve developed a little but it’s not working:
from tkinter import *
from tkinter import ttk
import sqlite3
#db_name = 'bancodados.db'
root = Tk()
root.geometry('600x500+340+50')
root.title('Cadastro')
root.resizable(False,False)
root.configure(bg='azure')
frame = LabelFrame(root, text='Adicionar Novo',bg='azure')
frame.grid(padx=200,pady=30)
lb = Label(frame,text='Nome:',width=15,bg='azure')
lb.grid(row=1,column=0)
lb2 = Label(frame, text='Preço:',bg='azure')
lb2.grid(row=2,column=0)
def add():
query = '''INSERT INTO produto(name, price) VALUES(?, ?)'''
cursor.execute(query, (name.get(), price.get()))
for row in cursor:
tree.insert('', 0, text=row[1], value=row[2])
cursor.execute('SELECT * FROM produto ORDER BY name DESC')
view()
conn.commit()
tree.mainloop()
def delete():
cursor.execute('DELETE FROM produto WHERE price = id',())
conn.commit()
for row in cursor:
tree.insert('', 0, text=row[1], value=row[2])
view()
tree.mainloop()
cursor.close()
i = Button(frame,text='Adicionar',bg='azure',command=add)
i.grid(row=4,column=1,pady=4)
o = Label(frame)
o.grid(row=0,column=0)
name = Entry(frame)
name.grid(row=1,column=1,padx=5)
price = Entry(frame)
price.grid(row=2,column=1,padx=5)
tree = ttk.Treeview(height=12,column=2)
tree.grid(row=1,column=0,columnspan=2)
tree.heading('#0', text='Nome',anchor=CENTER)
tree.heading('#1', text='Preço',anchor=CENTER)
#vs = ttk.Scrollbar()
#vs.grid()
#tree.configure(yscrollcommand=vs.set)
bt_ed = Button(text='Editar',bg='azure')
bt_ed.place(x=330,y=450)
bt_del = Button(text='Deletar',bg='azure',command=delete)
bt_del.place(x=270,y=450)
conn = sqlite3.connect('bancodados.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM produto ')
def view():
records = tree.get_children()
for element in records:
tree.delete(element)
for row in cursor:
tree.insert('', 0, text=row[1], value=row[2])
view()
root.mainloop()
"It’s not working" it’s kind of vague, right? It would be nice to [Dit], say what you expected, how you tested and what went wrong.
– Bacco
This Where you’re doing, 'price = id', shouldn’t it be 'price = ? ' and the id value is passed in the array, just like you did in Insert?
– Caio Augusto Papai