Probema从sqlite中删除tkinter

guz6ccqo  于 2023-05-18  发布在  SQLite
关注(0)|答案(2)|浏览(170)
import tkinter as tk
import sqlite3 as sql

class main_window:
    def __init__(self, window):
        self.wind = window
        self.wind.geometry('500x500')
        self.wind.resizable(0, 0)
        self.draw()
        self.refresh()

    def draw(self):
        tk.Label(text= 'Usuario').pack()
        self.user = tk.Entry()
        self.user.pack()

        tk.Label(text= 'Password').pack()
        self.password = tk.Entry()
        self.password.pack()

        self.frame = tk.Frame(self.wind)
        self.frame.pack()

        tk.Button(self.frame, text= 'Save', width= 5, height= 1, command= self.save).pack(side= 'left')
        tk.Button(self.frame, text= 'Delete', width= 5, height= 1, command= self.delete).pack(side= 'top')

        self.user_list = tk.Listbox(width= 40, height= 100)
        self.user_list.pack(padx= 4, pady= 8, side= 'left')

        self.password_list = tk.Listbox(width= 40, height= 100)
        self.password_list.pack(padx= 4, pady= 8, side= 'left')

    def refresh(self):
        self.user.delete(0, tk.END)
        self.password.delete(0, tk.END)

        self.user_list.delete(0, tk.END)
        self.password_list.delete(0, tk.END)
        
        db = sql.connect('accounts.db')
        curs = db.cursor()

        db_accounts_users = curs.execute("SELECT user FROM tb_accounts")
        for user in db_accounts_users:
            for uss in user:
                self.user_list.insert(tk.END, uss)

        db_accounts_pass = curs.execute("SELECT pass FROM tb_accounts")
        for password in db_accounts_pass:
            for fpass in password:
                self.password_list.insert(tk.END, fpass)

        db.commit()
        db.close()

    def save(self):
        db = sql.connect('accounts.db')
        curs = db.cursor()
        curs.execute("INSERT INTO tb_accounts(user, pass) VALUES (?, ?)", (self.user.get(), self.password.get()))
        db.commit()
        db.close()
        self.refresh()

    def delete(self):
        for user in self.user_list.curselection():
            select_user = self.user_list.get(user)
        db = sql.connect('accounts.db')
        curs = db.cursor()
        curs.execute("DELETE FROM tb_accounts WHERE user= ?", (select_user))
        db.commit()
        db.close()
        self.refresh()

obj_main_window = main_window(tk.Tk())
obj_main_window.wind.mainloop()

这个想法是用一个按钮删除一个数据。但是当我按下按钮时,我得到了下一个错误。sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用1,提供了4。
在错误中提供的数量取决于用户有多少个字符。谢谢你的帮助。这只是一个个人实践。

wgx48brx

wgx48brx1#

我使用list而不是tuple作为查询参数:
curs.execute(“DELETE FROM tb_accounts WHERE user=?“,[select_user])

pxiryf3j

pxiryf3j2#

请注意,(select_user)不是元组,但(select_user,)是。

curs.execute("DELETE FROM tb_accounts WHERE user= ?", (select_user,))

相关问题