-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLui.py
More file actions
317 lines (229 loc) · 8 KB
/
Copy pathSQLui.py
File metadata and controls
317 lines (229 loc) · 8 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from tkinter import *
from tkinter import ttk
import pymysql
from sqlalchemy import text, MetaData, create_engine
from sqlalchemy_utils import drop_database, database_exists, create_database
import SQLbase
## create db
global engine
engine = create_engine('postgresql://postgres:1597@localhost/proj', echo=True)
if not database_exists(engine.url):
create_database(engine.url)
SQLbase.load_db()
global metadata
metadata = MetaData(engine)
pymysql.install_as_MySQLdb()
conn = engine.connect()
meta = MetaData(bind=conn)
meta.reflect()
def delete_row():
selectedItem = tab.focus()
selectedItem = list(tab.item(selectedItem)['values'])
table = meta.tables[choose.get()]
columns = list(table.columns.keys())
delete_string = ""
for i in range(0, len(columns)):
delete_string += columns[i] + "='" + str(selectedItem[i]) + "' AND "
delete_string = delete_string[0:len(delete_string) - 4]
print(delete_string)
query = table.delete().where(text(delete_string))
conn.execute(query)
on_select()
def clear_db():
table = meta.tables[choose.get()]
query = table.delete()
conn.execute(query)
on_select()
def clear_all():
for tbl in reversed(meta.sorted_tables):
engine.execute(tbl.delete())
def add_row():
def add_query():
add_list = []
keys = entries.keys()
for i in keys:
add_list.append(str(entries[i].get()))
query = table.insert().values(tuple(add_list))
conn.execute(query)
on_select()
add.destroy()
add = Tk()
add.title("Add")
table = meta.tables[choose.get()]
entries = {}
table_cols = table.columns.keys()
red = 0
for col in table_cols:
label = Label(add, text=col, width=20)
label.grid(row=red, column=0)
entries[col] = Entry(add, width=30)
entries[col].grid(row=red, column=1)
red += 1
add_b = Button(add, text="Add", command=add_query, width=10)
add_b.grid(row=red, column=0, columnspan=2, pady=5)
def edit_row():
def edit_query():
edit_values = []
edit_where = ""
for j in range(0, len(col)):
edit_values.append(str(entries[col[j]].get()))
edit_where += col[j] + "='" + str(selectedItem[j]) + "' AND "
edit_where = edit_where[0:len(edit_where) - 4]
query = table.update().where(text(edit_where)).values(tuple(edit_values))
conn.execute(query)
on_select()
edit.destroy()
edit = Tk()
edit.title("Edit")
selectedItem = tab.focus()
selectedItem = list(tab.item(selectedItem)['values'])
table = meta.tables[choose.get()]
col = list(table.columns.keys())
entries = {}
for i in range(0, len(col)):
label = Label(edit, text=col[i], width=20)
label.grid(row=i, column=0)
entries[col[i]] = Entry(edit, width=30)
entries[col[i]].insert(0, str(selectedItem[i]))
entries[col[i]].grid(row=i, column=1)
edit_b = Button(edit, text="Save Editing", command=edit_query, width=10)
edit_b.grid(row=len(col), column=0, columnspan=2, pady=5)
def del_button():
re_button.destroy()
on_select()
def delete_db():
for tbl in reversed(meta.sorted_tables):
engine.execute(tbl.delete())
drop_database(engine.url)
on_select()
def delete_all():
delete_db()
on_select()
def delete_fi():
def show_delete_query():
query = f'''
delete
from {tabs}
where {entries[0].get()}='{entries[1].get()}'
'''
conn.execute(query)
on_select()
delete = Tk()
delete.title("DeleteFi")
selectedItem = tab.focus()
list(tab.item(selectedItem)['values'])
tabs = meta.tables[choose.get()]
entries = {}
label = Label(delete, text='Variable', width=20)
label.grid(row=0, column=0)
entries[0] = Entry(delete, width=30)
entries[0].grid(row=0, column=1)
label = Label(delete, text='Value', width=20)
label.grid(row=1, column=0)
entries[1] = Entry(delete, width=30)
entries[1].grid(row=1, column=1)
edit_b = Button(delete, text="Delete", command=show_delete_query, width=10)
edit_b.grid(row=2, column=0, columnspan=2, pady=5)
def find_in():
def show_find_query():
query = f'''
select *
from {tabs}
where {entries[0].get()}='{entries[1].get()}'
'''
result = conn.execute(query)
iid_counter = 0
for item in tab.get_children():
tab.delete(item)
for row in result:
row = list(row)
tab.insert(parent='', index='end', text="", iid=iid_counter, values=row)
iid_counter += 1
global re_button
re_button = Button(option_frame, text="Return", command=del_button, width=10)
re_button.pack(side=LEFT)
find.destroy()
find = Tk()
find.title("Find")
selectedItem = tab.focus()
list(tab.item(selectedItem)['values'])
tabs = meta.tables[choose.get()]
entries = {}
label = Label(find, text='Variable', width=20)
label.grid(row=0, column=0)
entries[0] = Entry(find, width=30)
entries[0].grid(row=0, column=1)
label = Label(find, text='Value', width=20)
label.grid(row=1, column=0)
entries[1] = Entry(find, width=30)
entries[1].grid(row=1, column=1)
edit_b = Button(find, text="Show", command=show_find_query, width=10)
edit_b.grid(row=2, column=0, columnspan=2, pady=5)
def on_select():
for item in tab.get_children():
tab.delete(item)
table = meta.tables[choose.get()]
tab.heading("#0", text="")
tab.column("#0", anchor=W, width=0)
tab['columns'] = list(table.columns.keys())
for col in list(table.columns.keys()):
tab.column(col)
for col in list(table.columns.keys()):
tab.heading(col, text=col, anchor=W)
iid_counter = 0
result = conn.execute(table.select())
for row in result:
row = list(row)
tab.insert(parent='', index='end', text="", iid=iid_counter, values=row)
iid_counter += 1
def load_db():
SQLbase.load_db()
global engine
engine = create_engine('postgresql://postgres:1597@localhost/proj', echo=True)
global metadata
metadata = MetaData(engine)
on_select()
## ui config
window = Tk()
window.title("MMORPG_MAKER")
window.geometry("1280x720")
choose = StringVar()
choose.set(list(meta.tables.keys())[0])
option_frame = Frame(window)
option = OptionMenu(option_frame, choose, *list(meta.tables.keys()), command=on_select)
option.pack(side=LEFT)
## main buttons
add_button = Button(option_frame, text="Add", command=add_row, width=10)
add_button.pack(side=LEFT)
edit_button = Button(option_frame, text="Edit", command=edit_row, width=10)
edit_button.pack(side=LEFT)
add_button = Button(option_frame, text="Find In", command=find_in, width=10)
add_button.pack(side=LEFT)
delete_button = Button(option_frame, text="Delete Fi", command=delete_fi, width=10)
delete_button.pack(side=LEFT)
delete_button = Button(option_frame, text="Delete Str", command=delete_row, width=10)
delete_button.pack(side=LEFT)
add_button = Button(option_frame, text="Delete All", command=delete_all, width=10)
add_button.pack(side=LEFT)
add_button = Button(option_frame, text="Clear Table", command=clear_db, width=10)
add_button.pack(side=LEFT)
add_button = Button(option_frame, text="Clear All", command=clear_all, width=10)
add_button.pack(side=LEFT)
add_button = Button(option_frame, text="Load Base", command=load_db, width=10)
add_button.pack(side=LEFT)
option_frame.pack(side=TOP)
## frame
tree_frame = Frame(window)
tab = ttk.Treeview(tree_frame)
tree_scroll_y = Scrollbar(window, orient=VERTICAL)
tree_scroll_x = Scrollbar(window, orient=HORIZONTAL)
tab.config(yscrollcommand=tree_scroll_y.set, xscrollcommand=tree_scroll_x.set)
tree_scroll_y.config(command=tab.yview)
tree_scroll_x.config(command=tab.xview)
tree_scroll_y.pack(side=RIGHT, fill=Y)
tree_scroll_x.pack(side=BOTTOM, fill=X)
tab.pack(fill=BOTH, expand=1)
tree_frame.pack(side=TOP, fill=BOTH, expand=1)
## app
on_select()
window.mainloop()