回溯(最近通话最近一次):
File "/Users/cyrilzyriacjoseph/Library/Preferences/PyCharmCE2019.2/scratches/scratch_5.py", line 46, in <module> db = DB() #instantiate Database File "/Users/cyrilzyriacjoseph/Library/Preferences/PyCharmCE2019.2/scratches/scratch_5.py", line 7, in __init__ self.conn=sqlite3.connect(students.db) NameError: name 'students' is not defined Exception ignored in: <function DB.__del__ at 0x109cf0b00> Traceback (most recent call last): File "/Users/cyrilzyriacjoseph/Library/Preferences/PyCharmCE2019.2/scratches/scratch_5.py", line 13, in __del__ self.conn.close() AttributeError: 'DB' object has no attribute 'conn'
#CODE
from tkinter import *
import sqlite3
from tkinter import messagebox
class DB():
def __init__(self):
self.conn=sqlite3.connect(students.db)
self.cursor=self.conn.cursor()
self.conn.execute("CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY,name TEXT ,college TEXT)")
self.commit()
def __del__(self):
self.conn.close()
def view(self):
self.cursor.execute("SELECT * FROM student")
row=self.cursor.fetchall() #get all rows from database
return row
def insert(self,name,college):
self.conn.execute("INSERT INTO student VALUES(NULL,?,?)",(name,college))
self.conn.commit()
self.view()
#search into database
def search(self,name="",college=""):
self.cursor.execute("SELECT * FROM student WHERE name=? OR college=?",(name,college))
found_rows=self.cursor.fetchall()
return found_rows
#update database
def update(self,name,college):
self.cursor.execute("UPDATE student SET name=?,college=? WHERE id=?",(name,college,id))
self.view()
#delete
def delete(self,id):
print(id)
self.cursor.execute("DELETE FROM student WHERE id=?",(id))
self.conn.commit()
self.view()
db = DB() #instantiate Database
def get_selected_row(event):
global selected_tuple
index = list1.curselection()[0]
selected_tuple = list1.get(index)
e1.delete(0, END)
e1.insert(END, selected_tuple[1])
e2.delete(0, END)
e2.insert(END, selected_tuple[2])
def view_command():
list1.delete(0, END)
for row in db.view():
list1.insert(END, row)
def search_command():
list1.delete(0, END)
for row in db.search(name.get(), college.get()):
list1.insert(END, row)
def add_command():
db.insert(name.get(), college.get())
list1.delete(0, END)
list1.insert(END, (name.get(), college.get()))
def delete_command():
db.delete(selected_tuple[0])
def update_command():
db.update(selected_tuple[0], name.get(), college.get())
#interface
window=Tk()
window.title("students data")
def on_closing():
dd = db
if messagebox.askokcancel("Quit", "Do you want to quit?"):
window.destroy()
del dd
window.protocol("WM_DELETE_WINDOW", on_closing) # handle window closing
l1 = Label(window, text="name")
l1.grid(row=0, column=0)
l2 = Label(window, text="college")
l2.grid(row=0, column=2)
name_text= StringVar()
e1 = Entry(window, textvariable=name_text)
e1.grid(row=0, column=1)
college_text= StringVar()
e2 = Entry(window, textvariable=college_text)
e2.grid(row=0, column=3)
list1 = Listbox(window, height=6, width=35)
list1.grid(row=2, column=0, rowspan=6, columnspan=2)
sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)
list1.bind('<<ListboxSelect>>', get_selected_row)
b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)
b2 = Button(window, text="Search entry", width=12, command=search_command)
b2.grid(row=3, column=3)
b3 = Button(window, text="Add entry", width=12, command=add_command)
b3.grid(row=4, column=3)
b4 = Button(window, text="Update selected", width=12, command=update_command)
b4.grid(row=5, column=3)
b5 = Button(window, text="Delete selected", width=12, command=delete_command)
b5.grid(row=6, column=3)
b6 = Button(window, text="Close", width=12, command=window.destroy)
b6.grid(row=7, column=3)
window.mainloop()