-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbconfigure.py
68 lines (63 loc) · 2.15 KB
/
dbconfigure.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
import mysql.connector
import json
def dbJSON_create(host, user, password, db):# This configures the database spesification to be used in app
jsonData = {
"host": host,
"user": user,
"password": password,
"db": db
}
with open('dbconfig.json', 'w') as outfile:
json.dump(jsonData, outfile, indent=4)
def configDB():
with open('dbconfig.json') as f:
data = json.loads(f.read())
somasdb = mysql.connector.connect(
host=data['host'],
user=data['user'],
passwd=data['password'],
auth_plugin='mysql_native_password',
database=data['db']
)
db_cursor = somasdb.cursor()
db_cursor.execute('SHOW TABLES;')
tables = db_cursor.fetchall()
all_tables=[]
for table in tables:
all_tables+=table
if(all_tables==['Administrator', 'Advised', 'Advisor', 'Attendee', 'Event', 'Event_Guest', 'Payment', 'Society', 'SocietyMembers', 'Student', 'User']):
return False
with open('etc/somasdb.sql', 'r') as f:
db_cursor.execute(f.read(), multi=True)
return True
if __name__ == '__main__':
host = input("Host: ")
user = input("User: ")
password = input("Password: ")
db = input("Database: ")
dbJSON_create(host, user, password, db)
flag = configDB()
with open('dbconfig.json') as f:
data = json.loads(f.read())
somasdb = mysql.connector.connect(
host=data['host'],
user=data['user'],
passwd=data['password'],
auth_plugin='mysql_native_password',
database=data['db']
)
db_cursor = somasdb.cursor()
if(flag):
print("Database configured successfully")
else:
print("Database already configured")
admin = input("Create an admin account [y/n]: ")
if(admin == 'y'):
admin_name = input("Admin name: ")
admin_email = input("Admin Email: ")
admin_pass = input("Admin Password: ")
db_cursor.execute(f'''INSERT INTO Administrator VALUES ('{admin_email}',"{admin_name}",MD5("{admin_pass}"));''')
somasdb.commit()
else:
print("Admin account not created.")
print("Database creation is done.")