Repository navigation
Expand file tree
/
Copy pathStudent_Database.py
More file actions
91 lines (69 loc) · 2.38 KB
/
Copy pathStudent_Database.py
File metadata and controls
91 lines (69 loc) · 2.38 KB
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
from tkinter import *
import tkinter as tk
import psycopg2
root = Tk()
root.title("Student Database")
def get_data(name, age, address) :
conn = psycopg2.connect(dbname="postgres", user="postgres", password="nitti2205", host="localhost")
cursor = conn.cursor()
query = '''INSERT INTO student (NAME,AGE,ADDRESS) VALUES (%s,%s,%s) ;'''
cursor.execute(query,(name,age,address))
print("Data Inserted")
conn.commit()
conn.close()
display_all()
def search(id) :
conn = psycopg2.connect(dbname="postgres", user="postgres", password="nitti2205", host="localhost")
cursor = conn.cursor()
query = '''SELECT * FROM student WHERE id=%s '''
cursor.execute(query,(id))
row = cursor.fetchone()
#calling function to display the fetched row
display_search(row)
conn.commit()
conn.close()
def display_search(row) :
listbox = Listbox(frame, width=20, height=1)
listbox.grid(row=9, column=1)
listbox.insert(END, row)
def display_all():
conn = psycopg2.connect(dbname="postgres", user="postgres", password="nitti2205", host="localhost")
cursor = conn.cursor()
query = '''select * from student'''
cursor.execute(query)
row = cursor.fetchall()
listbox = Listbox(frame, width=20, height=5)
listbox.grid(row=10, column=1)
for x in row :
listbox.insert(END, x)
canvas = Canvas(root, height=480, width=900)
canvas.pack()
frame = Frame()
frame.place(relx=0.3 , rely=0.1, relwidth=0.8 , relheight=0.8)
label = Label(frame, text="Add data")
label.grid(row=0, column=1)
label = Label(frame, text="Name")
label.grid(row=1, column=0)
entry_name = Entry(frame)
entry_name.grid(row=1,column=1)
label = Label(frame, text="Age")
label.grid(row=2, column=0)
entry_age = Entry(frame)
entry_age.grid(row=2,column=1)
label = Label(frame, text="Address")
label.grid(row=3, column=0)
entry_address = Entry(frame)
entry_address.grid(row=3,column=1)
button = Button(frame, text="Add", command= lambda : get_data(entry_name.get(), entry_age.get(), entry_address.get()))
button.grid(row=4, column=1)
#search by id
label = Label(frame, text="Search Data")
label.grid(row=5, column=1)
label = Label(frame, text="Search by ID")
label.grid(row=6, column=0)
id_search = Entry(frame)
id_search.grid(row=6,column=1)
button = Button(frame, text="Search", command=lambda:search(id_search.get()))
button.grid(row=6, column=2)
display_all()
root.mainloop()