-
Notifications
You must be signed in to change notification settings - Fork 345
/
cloud_db.py
118 lines (88 loc) · 2.65 KB
/
cloud_db.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
from tkinter import *
import psycopg2
# postgresql-cubic-98264
root = Tk()
root.title('Codemy.com - Postgres Cloud App')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x550")
def clear():
f_name.delete(0, END)
_name.delete(0, END)
def query():
# Configure and connect to Postgres
conn = psycopg2.connect(
host = "ec2-18-211-41-246.compute-1.amazonaws.com",
database = "dalilo11l7p24t",
user = "ikyumnardmfjok",
password = "52d3b09a2480693c2649d8c8a6b10514ec9798d08126844f16a06342480e215b",
port = "5432",
)
# Create a cursor
c = conn.cursor()
# Create a Table
c.execute('''CREATE TABLE IF NOT EXISTS customers
(first_name TEXT,
last_name TEXT);
''')
conn.commit()
conn.close()
def submit():
# Configure and connect to Postgres
conn = psycopg2.connect(
host = "ec2-18-211-41-246.compute-1.amazonaws.com",
database = "dalilo11l7p24t",
user = "ikyumnardmfjok",
password = "52d3b09a2480693c2649d8c8a6b10514ec9798d08126844f16a06342480e215b",
port = "5432",
)
# Create a cursor
c = conn.cursor()
# Insert data into table
thing1 = f_name.get()
thing2 = l_name.get()
c.execute('''INSERT INTO customers (first_name, last_name)
VALUES (%s, %s)''', (thing1, thing2)
)
conn.commit()
conn.close()
update()
clear()
def update():
# Configure and connect to Postgres
conn = psycopg2.connect(
host = "ec2-18-211-41-246.compute-1.amazonaws.com",
database = "dalilo11l7p24t",
user = "ikyumnardmfjok",
password = "52d3b09a2480693c2649d8c8a6b10514ec9798d08126844f16a06342480e215b",
port = "5432",
)
# Create a cursor
c = conn.cursor()
# Grab stuff from online database
c.execute("SELECT * FROM customers")
records = c.fetchall()
output = ''
# Loop thru the results
for record in records:
output_label.config(text=f'{output}\n{record[0]} {record[1]}')
output = output_label['text']
conn.close()
# Create The GUI For The App
my_frame = LabelFrame(root, text="Postgres Example")
my_frame.pack(pady=20)
f_label = Label(my_frame, text="First Name:")
f_label.grid(row=0, column=0, pady=10, padx=10)
f_name = Entry(my_frame, font=("Helvetica, 18"))
f_name.grid(row=0, column=1, pady=10, padx=10)
l_label = Label(my_frame, text="Last Name:")
l_label.grid(row=1, column=0, pady=10, padx=10)
l_name = Entry(my_frame, font=("Helvetica, 18"))
l_name.grid(row=1, column=1, pady=10, padx=10)
submit_button = Button(my_frame, text="Submit", command=submit)
submit_button.grid(row=2, column=0, pady=10, padx=10)
update_button = Button(my_frame, text="Update", command=update)
update_button.grid(row=2, column=1, pady=10, padx=10)
output_label = Label(root, text="")
output_label.pack(pady=50)
query()
root.mainloop()