-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path19_delete_data_sqlite3.py
127 lines (93 loc) · 3.47 KB
/
19_delete_data_sqlite3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from tkinter import *
import sqlite3
root = Tk()
root.title("delete data")
root.geometry("600x300")
# Creat a database or connect to one
conn = sqlite3.connect("MyDB.db")
# Create Cursor
cur = conn.cursor()
# Creat Table - Only Once
# cur.execute('CREATE TABLE students (first_name TEXT, last_name TEXT, course INTEGER, grade REAL)')
# Create Delete Function
def delete():
# Creat a database or connect to one
conn = sqlite3.connect("MyDB.db")
# Create Cursor
cur = conn.cursor()
# Delete record
cur.execute('DELETE FROM students WHERE oid='+delete_item.get())
# Commit changes
conn.commit()
# Close connection
conn.close()
# Create Submit function
def submit():
# Creat a database or connect to one
conn = sqlite3.connect("MyDB.db")
# Create Cursor
cur = conn.cursor()
# Insert Into Table
cur.execute('INSERT INTO students VALUES (:f_name, :l_name, :course, :grade)',
{'f_name': first_name.get(), 'l_name': last_name.get(), 'course': course.get(), 'grade': grade.get()})
# Commit changes
conn.commit()
# Close connection
conn.close()
# Clear Text Boxes
first_name.delete(0, END)
last_name.delete(0, END)
course.delete(0, END)
grade.delete(0, END)
# Create Query function
def query():
# Creat a database or connect to one
conn = sqlite3.connect("MyDB.db")
# Create Cursor
cur = conn.cursor()
# Query the database from the Table
cur.execute('SELECT *, oid FROM students')
# cur.fetchone()
# cur.fetchmany(10)
records = cur.fetchall()
# Loop Thru Results
print_records = ""
for rec in records:
print_records += str(rec[4])+'\t'+str(rec[0])+' '+str(rec[1])+' '+str(rec[2])+' '+str(rec[3])+"\n"
# Create and positioning Label for the Result
Label(root, text=print_records).grid(row=7, column=1, columnspan=6)
# Commit changes
conn.commit()
# Close connection
conn.close()
# Create and positioning Entry Boxes
first_name = Entry(root, width=15)
first_name.grid(row=1, column=1, padx=20, pady=(10, 0), sticky=W)
last_name = Entry(root, width=15)
last_name.grid(row=2, column=1, padx=20, sticky=W)
course = Entry(root, width=5)
course.grid(row=3, column=1, padx=20, sticky=W)
grade = Entry(root, width=5)
grade.grid(row=4, column=1, padx=20, sticky=W)
delete_item = Entry(root, width=5)
delete_item.grid(row=5, column=6, columnspan=1, sticky=W)
# Create and positioning Labels
Label(root, text="First Name").grid(row=1, column=0, pady=(10, 0), sticky=E)
Label(root, text="Last Name").grid(row=2, column=0, sticky=E)
Label(root, text="Course").grid(row=3, column=0, sticky=E)
Label(root, text="Grade").grid(row=4, column=0, sticky=E)
Label(root, text="ID #").grid(row=5, column=5, padx=20, sticky=E)
# Create and positioning Submit Button
Button(root, text="Add Record", command=submit).grid(row=6, column=1, columnspan=2, padx=20, pady=10, ipadx=15,
sticky=E)
# Create and positioning Query Button
Button(root, text="Show Records", command=query).grid(row=6, column=3, columnspan=2, padx=20, pady=10, ipadx=10,
sticky=E)
# Create and positioning Delete Button
Button(root, text="Delete Record", command=delete).grid(row=6, column=5, columnspan=2, padx=20, pady=10, ipadx=10,
sticky=E)
# Commit changes
conn.commit()
# Close connection
conn.close()
root.mainloop()