-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
94 lines (76 loc) · 2.95 KB
/
database.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
from dotenv import load_dotenv
import os
from typing import Optional
from mysql.connector.cursor_cext import CMySQLCursor
from mysql.connector.connection_cext import CMySQLConnection
import mysql.connector
load_dotenv()
cnx: Optional[CMySQLConnection] = None
cursor: Optional[CMySQLCursor] = None
def connect_mysql(first: bool = False):
global cnx, cursor
cnx = mysql.connector.connect(
user=os.getenv('NAME'),
password=os.getenv('PASSWORD')
)
cursor = cnx.cursor()
if not first:
use_database(os.getenv("DBNAME"))
return cursor
def disconnect_mysql():
global cnx, cursor
if cnx and cursor:
cnx.close()
cursor.close()
return
print("You have not connected MySQL yet.")
def create_database(db_name):
try:
cursor.execute(f"CREATE DATABASE {db_name}")
except mysql.connector.Error as err:
print(f'Failed creating {db_name} database.\n{err}')
print(f'Database {db_name} created successfully.')
use_database(db_name)
def create_table(db_name, table_name, settings):
use_database(db_name)
try:
cursor.execute(f"CREATE TABLE {table_name} ({settings})")
except mysql.connector.Error as err:
print(f'Failed creating {table_name} table.\n{err}')
print(f'Table {table_name} created successfully.')
def use_database(db_name):
try:
cursor.execute(f"USE {db_name}")
except mysql.connector.Error as err:
print(f'Failed using {db_name} database.\n{err}')
def drop_database(db_name):
try:
cursor.execute(f"DROP DATABASE {db_name}")
except mysql.connector.Error as err:
if err.errno != 1008:
print(f'Failed to drop the {db_name} database\n{err}')
exit(1)
print(f"You can't drop {db_name} database, cause it does not exists.")
else:
print(f'Database {db_name} successfully dropped.')
def commit():
cnx.commit()
if __name__ == '__main__':
db_name = os.getenv('DBNAME')
connect_mysql(first=True)
drop_database(db_name)
create_database(db_name)
create_table(db_name, 'users', "id INT UNSIGNED,"
"username varchar(20) NOT NULL UNIQUE,"
"language varchar(2) NOT NULL DEFAULT 'en',"
"has_perms boolean NOT NULL DEFAULT FALSE,"
"PRIMARY KEY(id)")
create_table(db_name, 'messages', "id INT UNSIGNED AUTOINCREMENT,"
"user_id INT UNSIGNED UNIQUE,"
"message text DEFAULT '',"
"FOREIGN KEY(user_id) REFERENCES users(id),"
"PRIMARY KEY(id)")
create_table(db_name, 'to_users', "id INT UNSIGNED AUTOINCREMENT,"
"user_id INT UNSIGNED,"
"to_user varchar()")
disconnect_mysql()