Python 如何从数据库中删除或编辑记录
我为我的英语不好提前道歉。。。 我是编程界的新手,所以我真的不知道自己在做什么。我正在尝试用Python制作一个基本的地址簿,Tkinter。我设法编写代码在数据库中添加记录,但无法编写代码删除或编辑所选记录。谢谢你的帮助Python 如何从数据库中删除或编辑记录,python,sqlite,tkinter,Python,Sqlite,Tkinter,我为我的英语不好提前道歉。。。 我是编程界的新手,所以我真的不知道自己在做什么。我正在尝试用Python制作一个基本的地址簿,Tkinter。我设法编写代码在数据库中添加记录,但无法编写代码删除或编辑所选记录。谢谢你的帮助 import datetime import tkinter from tkinter import * import tkinter as tk from tkinter import ttk from PIL import Image, ImageTk import sq
import datetime
import tkinter
from tkinter import *
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import sqlite3
def date_for_humans():
date = datetime.datetime.now().date()
date = str(date)
x = date.split("-")
return x[2] + "." + x[1] + "." + x[0] + "."
# creating windows for show contact
def add():
kontakti = Toplevel()
kontakti.title("Lista kontakta")
kontakti.iconbitmap(r"phonebookicon.ico")
kontakti.geometry("400x500")
kontakti.resizable(False, False)
def submit():
# create data base or connect to one
conn = sqlite3.connect("Imenik.db")
# create cursor
c = conn.cursor()
# insert into a table
c.execute("INSERT INTO Kontakti VALUES (:f_name, :l_name, :number)",
{
"f_name": f_name.get(),
"l_name": l_name.get(),
"number": number.get()
})
# commint changes
conn.commit()
conn.close()
f_name.delete(0,END)
l_name.delete(0, END)
number.delete(0, END)
# creating and coloring top frame
topframe = tk.Frame(kontakti, height=150, bg="#ffffff")
topframe.pack(fill=X)
# creating and coloring bottom farame
bottomframe = tk.Frame(kontakti, height=350, bg="#34ebeb")
bottomframe.pack(fill=X)
# creating text at the top of app:
text2 = Label(kontakti, text="DODAVANJE KONTAKTA", font="ariel 15 bold", bg="#ffffff", fg="black")
text2.place(x=80, y=20)
# creating close button
button1 = Button(kontakti, text="Zatvori prozor", bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=kontakti.destroy)
button1.place(x=295, y=465)
# create text boxes
f_name = Entry(kontakti, width=30)
f_name.place(x=80, y=200, height=20)
l_name = Entry(kontakti, width=30)
l_name.place(x=80, y=230, height=20)
number = Entry(kontakti, width=30)
number.place(x=80, y=260, height=20)
# create text box labels
f_name_label = Label(kontakti, text="Ime", bg="#34ebeb")
f_name_label.place(x=20, y=200)
l_name_label = Label(kontakti, text="Prezime", bg="#34ebeb")
l_name_label.place(x=20, y=230)
number_label = Label(kontakti, text="Broj", bg="#34ebeb")
number_label.place(x=20, y=260)
# create sumbit button
submint_btn = Button(kontakti, text="Dodaj kontakt", bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=submit)
submint_btn.place(x=40, y=320)
def edit():
c.execute("UPDATE Kontakti SET f_name=?, l_name=?, number=? WHERE f_name= ? AND l_name = ? AND number=?",
(new_value_for_f_name, new_value_for_l_name, new_value_for_number, f_name, l_name, number))
conn.commit()
def delete():
f_name = listbox.curselection()[0]
l_name = listbox.curselection()[1]
number = listbox.curselection()[2]
c.execute("DELETE * FROM Kontakti WHERE f_name = ? AND l_name = ? AND number = ?", (f_name, l_name, number))
conn.commit()
def leave():
root.destroy()
# creating a main window:
root = Tk()
root.title("Imenik App")
root.iconbitmap(r"phonebookicon.ico")
root.geometry("650x550")
root.resizable(False, False)
# creating and coloring top frame:
topFrame = tk.Frame(root, height=150, bg="#ffffff")
topFrame.pack(fill=X)
# creating and coloring bottom frame:
bottomFrame = tk.Frame(root, height=500, bg="#34ebeb")
bottomFrame.pack_propagate(False)
bottomFrame.pack(fill=X)
listbox = Listbox(bottomFrame)
listbox.place(x=40, y=40, height=340, width=200)
scrollbar = Scrollbar(bottomFrame)
scrollbar.place(height=340, x=240, y=40)
# Insert elements into the listbox
conn = sqlite3.connect("Imenik.db")
c = conn.cursor()
a = c.execute("SELECT *,oid FROM Kontakti")
records = c.fetchall()
for record in records:
listbox.insert(END, str(record[0]) + " " + str(record[1]))
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# creating text at the top of app:
text1 = Label(root, text="IMENIK", font="ariel 35 bold", bg="#ffffff", fg="black")
text1.place(x=240, y=40)
# displaying date and time at the top of app:
datel = Label(root, text="Danasnji datum: " + date_for_humans(), font="ariel 10 bold", bg="#ffffff", fg="black")
datel.place(x=450, y=125)
# displaying icon at the top of the app:
image1 = Image.open("phonebook1.png")
image1 = image1.resize((90, 90), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image1)
label1 = tkinter.Label(image=image, bg="#ffffff")
label1.image = image
label1.place(x=80, y=30)
conn.commit()
conn.close()
'''
# create data base or connect to one
conn = sqlite3.connect("Imenik.db")
# create cursor
c = conn.cursor()
# create table
c.execute("""CREATE TABLE kontakti (
first_name txt,
last_name txt,
number integer
)""")
# commint changes
conn.commit()
conn.close()
'''
# adding button 1, add contacts
viewButton = Button(root, text="Dodaj kontakt", pady=10, padx=70, bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=add)
viewButton.place(x=380, y=200)
# adding button 2, edit contacts
addButton = Button(root, text="Izmeni kontakt", pady=10, padx=67, bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=edit)
addButton.place(x=380, y=260)
# adding button 3, delete contacts
deleteButton = Button(root, text="Obrisi kontakt", pady=10, padx=70, bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=delete)
deleteButton.place(x=380, y=320)
# adding button 4, exit button
exitButton = Button(root, text="Izlaz", pady=5, padx=50, bg="white", fg="black",
activebackground="#A9A9A9", font=("Helvetica", 10, "bold"), command=leave)
exitButton.place(x=505, y=510)
root.mainloop()
如果有什么不清楚的,请问我真的需要帮助。
提前谢谢 您使用的是文字sql脚本,基本上可以像下面这样删除和更新 通知表单对象关系映射比原始查询更容易实现 删除 删除其中Id= conn=sqlite3.connectivenik.db c=连接光标 sql=删除Id=? id=1 c、 executesql,id, 更新 更新集=其中Id= conn=sqlite3.connectivenik.db c=连接光标 sql=更新表名集ColumnName=?其中Id=? id=1 新的_值='hello' 参数顺序从左到右很重要。 c、 executesql,新值,id
大家好,欢迎来到Stack Overflow。要了解使用鼠标选择的数据,可以使用以下方法:
name_of_listbox.curselection()
这将返回当前选定行的内容列表。它给了我们一个列表,因为有一个选项可以在列表框中选择多行数据。
您可以遍历每个对象并从中获取数据。
将数据存储在变量中,并将其用于UPDATE和DELETE命令
编辑:
要删除记录,请执行以下操作:
f_name = listbox.curselection()[0]
l_name = listbox.curselection()[1]
number = listbox.curselection()[2]
c1.execute("DELETE * FROM Kontakti WHERE f_name = ? AND l_name = ? AND number = ?", (f_name, l_name, number))
conn1.commit()
要修改值,请执行以下操作:
c1.execute("UPDATE Kontakti SET f_name=?, l_name=?, number=? WHERE f_name= ? AND l_name = ? AND number=?",(new_value_for_f_name,new_value_for_l_name,new_value_for_number, f_name, l_name, number))
conn1.commit()
我完全不理解您从单个列表框中选择名字、姓氏和数字的方式。因此,我保持了列表框的名称不变。更新或删除的条件是什么?如果要从表中删除行,则需要一个SQL delete语句,该语句的调用方式与代码执行insert的方式基本相同。更新也是如此。不要只是复制和粘贴别人的代码而不理解它们的含义。你需要在中传递tuple/list中的值。执行:c.executesql,id,和c.executesql,new_value,id。没错,现在修复了它。谢谢你的关注@ACW1668我可能错了,但我不认为这会满足我的要求。让我试着解释清楚。我已经显示了从数据库到列表框的所有记录,我希望从那里能够删除或更新。例如,我在列表框中显示了一条记录,当我点击它时,它被选中,从那里我想做一个按钮,该按钮将删除或打开新窗口来更新所选记录的名称/姓氏/编号。谢谢你的帮助,你能写下我的代码在我的案例中是什么样子吗?我当然会非常感激的!给我一点时间,如果你觉得我的答案是最好的,请把它说成是最好的。谢谢:我很抱歉,但删除它给我这个错误:索引器:元组索引超出范围。对于更新:name错误:未定义名称“new_value_for_f_name”。我可能做错了什么,因为我说我是编程新手。如果有帮助的话,我会编辑我的问题并把整个代码放进去。