更新和删除功能的问题

时间:2020-07-20 13:57:23

标签: python sqlite tkinter

我有一个简单的程序“电话簿”。这些功能中的两个功能之一是更新一个记录功能,但是当我尝试更新单个记录/行时,它将更新所有插入一个记录的记录。我还具有单个选定行的删除功能,当我按删除按钮时,它将引发以下错误:

c.execute("DELETE FROM profile WHERE First=?, Surname=?, phone_number=?") sqlite3.OperationalError: near ",": syntax error. 

这是我的代码:

def create_sql():
conn = sqlite3.connect("phone_book.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile (First TEXT, Surname TEXT, phone_number TEXT)")
conn.commit()
conn.close()
def get_row(event):
    try:
        cursor = tree.focus()
        content = tree.item(cursor)
        row = content["values"]
        one.set(row[0])
        two.set(row[1])
        three.set(row[2])
    except IndexError:
        pass
def update():
data1 = one.get()
data2 = two.get()
data3 = three.get()
cursor = tree.focus()
content = tree.item(cursor)
row = content["values"]
one.set(row[0])
two.set(row[1])
three.set(row[2])
conn = sqlite3.connect("phone_book.db")
cur = conn.cursor()
cur.execute("""UPDATE profile SET First=?, Surname=?, phone_number=?""", (data1, data2, data3,tree.set(row)))
conn.commit()
conn.close()
def del_sel():
    try:
        selected_item = tree.selection()[0] ## get selected item
        tree.delete(selected_item)
    except IndexError:
        pass
    conn = sqlite3.connect("phone_book.db")
    c = conn.cursor()
    c.execute("DELETE FROM profile WHERE First=? AND Surname=? AND phone_number=?"(data1, data2, data3) )
    conn.commit()
    conn.close()
def add_contact():
    if e.get() == "" and e1.get() =="" and e2.get() =="":
        messagebox.showwarning("Input Error","Przyjmniej jedno pole\nmusi być wypełnione")
    else:
        conn = sqlite3.connect("phone_book.db")
        c = conn.cursor()
        c.execute("INSERT INTO profile VALUES(:e,:e1,:e2)",
                {
                    'e':e.get(),
                    'e1':e1.get(),
                    'e2':e2.get()
                }
        )
        conn.commit()
        conn.close()
        tree.insert("",'end',values=(e.get(),e1.get(),e2.get()))
        e.delete(0,END)
        e1.delete(0,END)
        e2.delete(0,END)
    e = Entry(root,bd=2,textvariable=one)
e.grid(row=0,column=1,padx=10,pady=5)
e1 = Entry(root,bd=2,textvariable=two)
e1.grid(row=1,column=1,padx=10,pady=5)
e2 = Entry(root,bd=2,textvariable=three)
e2.grid(row=2,column=1,padx=10,pady=5)
e3 = Entry(root,bd=2)
e3.grid(row=5,column=1,pady=5)

tree = ttk.Treeview(root,height=10)
tree["columns"]=("one","two","three")
tree.column("one",width=120)
tree.column("two",width=130)
tree.column("three",width=160)
tree.heading("one", text="Imię")
tree.heading("two", text="Nazwisko")
tree.heading("three", text="Nr Telefonu")
tree["show"]="headings"
tree.grid(row=4,column=0,columnspan=6,padx=20)

1 个答案:

答案 0 :(得分:1)

update语句应具有WHERE子句,如下所示:

UPDATE profile SET First=?, Surname=?, phone_number=? WHERE id=?

,因此仅更新必须作为第4个参数传递的带有id的行。
id更改为作为表主键的列的名称。

或者可以使用First子句中的列SurnameWHERE

UPDATE profile SET First=?, Surname=?, phone_number=? WHERE First=? AND Surname=?

这意味着您必须将要更新的行的名称和姓氏作为第4和第5参数传递。

对于DELETE语句,正确的语法是:

DELETE FROM profile WHERE First=? AND Surname=? AND phone_number=?

,您还应该像为?语句那样为UPDATE占位符传递3个参数。