-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_account_currency.py
211 lines (161 loc) · 7.32 KB
/
opt_account_currency.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
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
#connection
import sqlalchemy as s
from connect import engine
from connect import connection
from connect import metadata
# create table object, called selected table, st
st = s.Table("account_currency", metadata, autoload=True, autoload_with=engine) #account_currency
# the functions that are imported into the display table and view table, adapt/change this when updating a table.
#________________________________________________________________________________________________________________________
def select_table():
# the query object
query = s.select([st.columns.account_currency_id, st.columns.account_currency_type])
# execute query
select_result_proxy = connection.execute(query)
return select_result_proxy
def update_table():
id_selection = int(input("Select result id: "))
value_selection = input("New result type: ")
update = s.update(st).values(account_currency_type=value_selection).where(st.columns.account_currency_id == id_selection)
proxy = connection.execute(update)
ans = "selected id updated"
return ans
def delete_table():
id_selection = int(input("Select result id: "))
query = s.delete(st).where(st.columns.account_currency_id == id_selection)
proxy = connection.execute(query)
ans = "selected id deleted"
return ans
def insert_table():
value_selection = input("New result type: ")
insert = s.insert(st).values(account_currency_type =value_selection)
proxy = connection.execute(insert)
ans = f"{value_selection} inserted"
# the functions can be imported into another table
#________________________________________________________________________________________________________________________
def display_table(table_name):
"""View , select, update and insert the table:
To adopt this function to suit other table; Change all occurencies for
1) selected_table.columns.[column_a]
2) selected_table.columns.[column_b]
with the new column names
To allow the user to insert into more columns (more than one),modify the insert section.
Args:
table_name (str): the name of the table
Returns:
_type_: the id of the entry selected
"""
try_again = True
while try_again:
select = select_table()
# STORING RESULTS PROXY IN A DICT
#_______________________________________________________________________________________
# record the output in a dict ( key and value); id and the name
output_dict = {}
for result in select:
#convert tuple output to one item
column_a = result[0]
column_b= result[1]
# table_id (primary key) recorded as the key, followed by identifier name
output_dict[column_a]=str.upper(column_b)
# insert option to update if input is missing
changes = {"u":"update","d":"delete","i":"insert" }
# DISPLAY ENTRIES IN DATABASE
#_______________________________________________________________________________________
print()
print(F"{table_name}")
print("***************************************************")
for key, value in output_dict.items():
print(f"{key}:{value}")
print("___________________________________________________")
# DISPLAY CREATE, DELETE AND INSERT OPTIONS
#_______________________________________________________________________________________
for key, value in changes.items():
print(f"<> ({key}):{value} <> ", end="")
print()
print("***************************************************")
user_selection = input("select: ")
# SELECT ENTRY IN DATABASE LOGIC
#_______________________________________________________________________________________
if user_selection.isdigit():
user_selection = int(user_selection)
if user_selection in output_dict.keys():
try_again = False
else:
print("Integer selected out of range")
try_again = True
# UPDATE , DELETE AND INSERT LOGIC
#_______________________________________________________________________________________
else:
if user_selection in changes.keys():
#UPDATE
if user_selection == str.lower("u"):
print()
print("_____________________________")
print("Updating a database entry")
print("_____________________________")
update_table()
print("_____________________________")
print()
try_again = True
#DELETE
elif user_selection == "d":
# delete the entries
print()
print("_____________________________")
print("Deleting a database entry")
print("_____________________________")
delete_table()
print("_____________________________")
print()
try_again = True
# INSERT
elif user_selection == "i":
# insert new entries
print()
print("_____________________________")
update_table()
print("_____________________________")
try_again = True
else:
print("Input selected is out of range")
# RETURN ID(PRIMARY KEY) OF SELECTED OPTION
#_______________________________________________________________________________________
return user_selection
def show_table(table_name):
try_again = True
while try_again:
select = select_table()
# STORING RESULTS PROXY IN A DICT
#_______________________________________________________________________________________
# record the output in a dict ( key and value); id and the name
output_dict = {}
for result in select:
#convert tuple output to one item
column_a = result[0]
column_b= result[1]
# table_id (primary key) recorded as the key, followed by identifier name
output_dict[column_a]=str.upper(column_b)
# DISPLAY ENTRIES IN DATABASE
#_______________________________________________________________________________________
print()
print(F"{table_name}")
print("***************************************************")
for key, value in output_dict.items():
print(f"{key}:{value}")
print("___________________________________________________")
# SELECT ENTRY IN DATABASE LOGIC
#_______________________________________________________________________________________
user_selection = input("select: ")
if user_selection.isdigit():
user_selection = int(user_selection)
if user_selection in output_dict.keys():
try_again = False
else:
print("Integer selected out of range")
try_again = True
else:
print("Wrong input")
return user_selection
if __name__ == "__main__":
show_table("account_currency")