add: init the database, add: init the user table
This commit is contained in:
parent
fe109bce1b
commit
d2d6fc9947
44
Database.py
Normal file
44
Database.py
Normal file
@ -0,0 +1,44 @@
|
||||
import mysql.connector
|
||||
import User
|
||||
|
||||
class Database:
|
||||
def __init__(self, host, port, user, password):
|
||||
"""
|
||||
: param host: The ip of the database
|
||||
: type host: str
|
||||
: param port: The port of the database
|
||||
: type port: int
|
||||
: param user: The user to login on the database
|
||||
: type user: str
|
||||
: param password: The password of the user
|
||||
: type password: str
|
||||
"""
|
||||
self.mydb = mysql.connector.connect(
|
||||
host = host,
|
||||
port = port,
|
||||
user = user,
|
||||
password = password,
|
||||
)
|
||||
self.cursor = self.mydb.cursor
|
||||
|
||||
def _create_table(self, name: str, content: dict):
|
||||
command: str = f"CREATE TABLE {name} ("
|
||||
for key, value in content.keys():
|
||||
command += f"{key} {value}, "
|
||||
command += ")"
|
||||
|
||||
def create_user_table(self):
|
||||
self.cursor.execute("SHOW TABLES");
|
||||
if (not "users" in self.cursor):
|
||||
data = {
|
||||
"email": "VARCHAR(255)",
|
||||
"username": "VARCHAR(255)",
|
||||
"first_name": "VARCHAR(255)",
|
||||
"last_name": "VARCHAR(255)",
|
||||
"password": "VARCHAR(255)"
|
||||
}
|
||||
self._create_table("users", data)
|
||||
|
||||
def add_user(self, user: User):
|
||||
self.cursor(user.to_insert_sql())
|
||||
|
6
User.py
6
User.py
@ -17,3 +17,9 @@ class User:
|
||||
self.username: str = username;
|
||||
self.password: str = password;
|
||||
self.permission = permissions;
|
||||
|
||||
def to_insert_sql(self):
|
||||
"""
|
||||
return text to add the user in the database
|
||||
"""
|
||||
return (f"INSERT INTO users (email, username, first_name, last_name, password) VALUES ({self.email}, {self.username}, {self.first_name}, {self.last_name}, {self.password})")
|
||||
|
8
config.json
Normal file
8
config.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"database": {
|
||||
"ip": "localhost",
|
||||
"port": 3306,
|
||||
"user": "root",
|
||||
"password": "bozo"
|
||||
}
|
||||
}
|
7
config.py
Normal file
7
config.py
Normal file
@ -0,0 +1,7 @@
|
||||
import json
|
||||
|
||||
f = open('config.json')
|
||||
|
||||
data = json.load(f)
|
||||
|
||||
database = data.get("database");
|
9
database.py
Normal file
9
database.py
Normal file
@ -0,0 +1,9 @@
|
||||
import config
|
||||
import Database
|
||||
|
||||
host = config.database.get("ip")
|
||||
port = config.database.get("port")
|
||||
user = config.database.get("user")
|
||||
password = config.database.get("password")
|
||||
|
||||
database = Database.Database(host, port, user, password)
|
Loading…
Reference in New Issue
Block a user