-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql3.py
106 lines (82 loc) · 2.86 KB
/
sql3.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
import tkinter as tk
from tkinter import ttk
import pymysql
# Establish a connection to the MySQL database
conn = pymysql.connect(
host='localhost',
user='root',
password='saloni',
database='userdata'
)
cursor = conn.cursor()
# Function to fetch data from the database and populate the Treeview
def load_data():
# Clear existing data in the Treeview
tree.delete(*tree.get_children())
# Fetch data from the table
cursor.execute("SELECT * FROM bd2")
rows = cursor.fetchall()
# Populate the Treeview
for row in rows:
tree.insert('', 'end', values=row)
def search_data():
query = search_entry.get().lower()
if query:
for item_id in tree.get_children():
values = tree.item(item_id, 'values')
if any(query in str(value).lower() for value in values):
tree.selection_add(item_id)
else:
tree.selection_remove(item_id)
else:
tree.selection_remove(*tree.get_children())
# Create the main window
root = tk.Tk()
root.title('PyMySQL Database Table Viewer')
# Create a search bar
search_frame = tk.Frame(root)
search_frame.pack(pady=10)
search_label = tk.Label(search_frame, text='Search:')
search_label.pack(side='left')
search_entry = tk.Entry(search_frame, width=30)
search_entry.pack(side='left')
search_button = tk.Button(search_frame, text='Search', command=search_data)
search_button.pack(side='left')
# Create a Treeview widget
tree = ttk.Treeview(root, columns=('ID','Username','Date', 'Pickup','Drop_loc','Distance','Bill'), show='headings')
tree.heading('ID', text='ID')
tree.heading('Username', text='Username')
tree.heading('Date', text='Date')
tree.heading('Pickup', text='Pickup')
tree.heading('Drop_loc', text='Drop_loc')
tree.heading('Distance', text='Distance')
tree.heading('Bill', text='Bill')
tree.pack()
tree.column('ID', width=50)
tree.column('Username', width=200)
tree.column('Date', width=220)
tree.column('Pickup', width=100)
tree.column('Drop_loc', width=100)
tree.column('Distance', width=200)
tree.column('Bill', width=100)
# Create a vertical scrollbar and associate it with the Treeview
vsb = ttk.Scrollbar(root, orient='vertical', command=tree.yview)
tree.configure(yscrollcommand=vsb.set)
# Pack the Treeview and scrollbar
tree.pack(side='left', fill='both', expand=True)
vsb.pack(side='right', fill='y')
# Load data into the Treeview
load_data()
# Button to refresh data
refresh_button = tk.Button(root, text='Refresh Data', command=load_data)
refresh_button.place(x=1280,y=10)
def w1():
root.destroy()
import adminwel
refresh_button1 = tk.Button(root, text='Back', command=w1)
refresh_button1.place(x=1240,y=10)
# Run the main loop
root.mainloop()
# Close the cursor and database connection
cursor.close()
conn.close()