-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
231 lines (212 loc) · 5.67 KB
/
modules.py
File metadata and controls
231 lines (212 loc) · 5.67 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
import sqlite3
import re
from prettytable import from_db_cursor
import os
import time
import uuid
conn = sqlite3.connect('students.db')
c = conn.cursor()
cr = """
CREATE TABLE IF NOT EXISTS students(id VARCHAR(20) PRIMARY KEY NOT NULL,
name VARCHAR(30) NOT NULL,
age INTEGER NOT NULL,
email VARCHAR(40) NOT NULL
)
"""
c.execute(cr)
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
msg = """
Choose An Option:
1 : Show Current Data
2 : Insert Data
3 : Delete Data
4 : Update Data
999999 : Clear all data
0 : Exit
"""
def show():
clear()
c = conn.cursor()
a = c.execute("""
SELECT * FROM students;
""")
mytable = from_db_cursor(a)
print(mytable)
def clear():
try:
os.system('cls')
except:
os.system('clear')
def inp(name,age,email):
c = conn.cursor()
try:
i = (str(uuid.uuid4())[0:8])
n = input("Enter YES to confirm : ")
if n.lower() == 'yes' or n.lower == 'y':
c.execute(f"""
INSERT INTO students(id,name,age,email) VALUES('{i}','{name}', {age}, '{email}');
""")
conn.commit()
clear()
show()
c.close()
else:
conn.rollback()
print("No changes were made..")
except sqlite3.Error as er:
print('SQLite error: %s' % (' '.join(er.args)))
show()
def check(email):
if(re.search(regex,email)):
return True
else:
return False
def destroy():
print("Doing this will delete all the data. \nAre you sure you want to continue?")
n = input("""Enter 'CONFIRM' : """)
if n == 'CONFIRM':
c = conn.cursor()
st = """DROP TABLE students"""
c.execute(st)
conn.commit()
c.close()
create_table()
show()
else:
print("Abort!")
def create_table():
c = conn.cursor()
clear()
st = """
CREATE TABLE students(id VARCHAR(20) PRIMARY KEY NOT NULL,
name VARCHAR(30) NOT NULL,
age INTEGER NOT NULL,
email VARCHAR(40) NOT NULL
)
"""
c.execute(st)
def remove():
clear()
show()
i = (input("Enter ID to remove: "))
c.execute(f"""SELECT name FROM students WHERE id = '{i}' """)
rows = c.fetchall()
name = rows[0][0]
clear()
print(f"""Are you sure you want to remove '{name}' from the table? (y/N)""")
ch = input()
if not(ch):
print("No changes were made.")
elif ch.lower() == 'y' or ch.lower() == 'yes':
print("OK....");
ct = f"""DELETE FROM students WHERE id ='{i}' """
c.execute(ct)
conn.commit()
print('Done!')
time.sleep(1)
clear()
show()
def update():
clear()
show()
i = (input("Enter ID of Student to update: "))
c.execute(f"""SELECT name FROM students WHERE id = '{i}' """)
rows = c.fetchall()
name = rows[0][0]
print('Name: ', name)
print("""
Choose what to update:
1 : Name
2 : Age
3 : Email-id
""")
a = int(input("Choice: "))
if a==1:
new = input("Enter new name: ")
st = str(f"CAUTION! This will change the name from {rows[0][0]} to {new} \n\nContinue(y/N)")
ch = input(st)
if ch.lower() == 'y' or ch.lower() == 'yes':
st = f"""UPDATE students
SET name = '{new}' WHERE id = '{i}'
"""
c.execute(st)
conn.commit()
show()
else:
print("Abort!")
time.sleep(2)
clear()
show()
elif a == 2:
c.execute(f"""SELECT name FROM students WHERE id = '{i}' """)
rows = c.fetchall()
age = rows[0][0]
new = int(input("Enter new age: "))
st = str(f"CAUTION! This will change the name from {age} to {new} \n\nContinue(y/N)")
ch = input(st)
if ch.lower() == 'y' or ch.lower() == 'yes':
st = f"""UPDATE students
SET age = {new} WHERE id = '{i}'
"""
c.execute(st)
conn.commit()
show()
else:
print("Abort!")
time.sleep(2)
clear()
show()
elif a == 3:
c.execute(f"""SELECT name FROM students WHERE id = '{i}' """)
rows = c.fetchall()
email = rows[0][0]
new = input("Enter new email: ")
if (new):
st = str(f"CAUTION! This will change the email from {email} to {new} \n\nContinue(y/N)")
ch = input(st)
if ch.lower() == 'y' or ch.lower() == 'yes':
st = f"""UPDATE students
SET email = '{new}' WHERE id = '{i}'
"""
c.execute(st)
conn.commit()
show()
else:
print("Abort!")
time.sleep(2)
clear()
show()
def main():
while True:
print(msg)
choice = int(input("Enter : "))
if choice == 1:
clear()
show()
elif choice == 0:
c.close()
clear()
exit()
elif choice == 2:
clear()
name = input("Enter name: ")
age = int(input("Enter age: "))
email = input("Enter email: ")
print("validating email....")
a = check(email)
if (a):
inp(name,age,email)
else:
print("Invalid email format!")
elif choice == 3:
clear()
remove()
elif choice == 4:
clear()
update()
elif choice == 999999:
clear()
destroy()
def start():
clear()
main()