Eh, kinda working
This commit is contained in:
43
db.py
Normal file
43
db.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import mysql.connector
|
||||
import os
|
||||
|
||||
def get_db():
|
||||
return mysql.connector.connect(
|
||||
host=os.getenv("MYSQL_HOST"),
|
||||
user=os.getenv("MYSQL_USER"),
|
||||
password=os.getenv("MYSQL_PASSWORD"),
|
||||
database=os.getenv("MYSQL_DATABASE"),
|
||||
autocommit=True
|
||||
)
|
||||
|
||||
def init_db():
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
discord_id VARCHAR(32) PRIMARY KEY,
|
||||
val_tag VARCHAR(32) NOT NULL,
|
||||
rank VARCHAR(32) NOT NULL,
|
||||
rr INT DEFAULT 0,
|
||||
kills INT DEFAULT 0,
|
||||
deaths INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS workouts (
|
||||
discord_id VARCHAR(32),
|
||||
workout VARCHAR(32),
|
||||
amount INT DEFAULT 0,
|
||||
PRIMARY KEY (discord_id, workout),
|
||||
FOREIGN KEY (discord_id)
|
||||
REFERENCES users(discord_id)
|
||||
ON DELETE CASCADE
|
||||
)
|
||||
""")
|
||||
|
||||
cur.close()
|
||||
db.close()
|
||||
print("✅ Database ready")
|
||||
Reference in New Issue
Block a user