Skip to content

Commit b735bc2

Browse files
authored
Add files via upload
DBTableManager.py is a semi API which is an 'in-progress piece of code' which aims to further simplify the use of the SQL API and Python codes
1 parent e41cb8e commit b735bc2

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Diff for: DBTableManager.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import mysql.connector
2+
import time
3+
import sys
4+
5+
conobj_var = ''
6+
7+
def login(db_pass):
8+
'''
9+
This function runs the login process, giving the user 3 chances to enter the password.
10+
If the password is entered incorrectly 3 times, the programs exits.
11+
'''
12+
13+
global conobj_var
14+
15+
time_delay = 5
16+
login_count = 1
17+
for i in range(0,3):
18+
try:
19+
conobj = mysql.connector.connect(host = 'localhost', user = 'root', password = db_pass)
20+
conobj_var = conobj
21+
22+
if conobj.is_connected:
23+
print('Connected Successfully')
24+
conobj_var = conobj
25+
break
26+
27+
except mysql.connector.errors.ProgrammingError:
28+
login_count += 1
29+
time.sleep(time_delay)
30+
31+
if login_count >= 3:
32+
print('Exiting Program, password entered incorrectly')
33+
sys.exit(1)
34+
break
35+
36+
37+
def db_creator(db_name):
38+
'''
39+
This function creates the DataBase.
40+
If absent, it creates the DataBase,
41+
else, it uses the existing database.
42+
'''
43+
44+
cur = conobj_var.cursor()
45+
cur.execute('show databases')
46+
db_present = cur.fetchall()
47+
48+
if (db_name,) in db_present:
49+
print('Database: {} is present in the client'.format(db_name))
50+
print('\nSwitching to DataBase: {}'.format(db_name))
51+
cur.execute('use {}'.format(db_name))
52+
53+
else:
54+
print('The database does not exist in the system. Do you want to create it?(Y/n): ')
55+
db_create = input()
56+
57+
if db_create.lower() == 'y':
58+
cur.execute('create database {}'.format(db_name))
59+
cur.execute('commit')
60+
cur.execute('use {}'.format(db_name))
61+
62+
else:
63+
print('Exiting Program')
64+
cur.close()
65+
sys.exit(1)
66+
67+
68+
def db_remover(db_name):
69+
'''
70+
This function removes the DataBase.
71+
If absent, it displays an error message,
72+
else, it deletes the DataBase.
73+
'''
74+
75+
cur = conobj_var.cursor()
76+
cur.execute('show databases')
77+
db_present = cur.fetchall()
78+
79+
if (db_name,) in db_present:
80+
print('DataBase: {} is present in the Client'.format(db_name))
81+
print('\nRemoving DataBase, this action cannot be undone')
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+

0 commit comments

Comments
 (0)