Datenbank verbindung + User Admin angelegt
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import json
|
||||
import mysql.connector
|
||||
|
||||
def lade_db_config(pfad='config/db_config.json'):
|
||||
with open(pfad, 'r') as f:
|
||||
return json.load(f)
|
||||
|
||||
def speichere_db_config(daten, pfad='config/db_config.json'):
|
||||
with open(pfad, 'w') as f:
|
||||
json.dump(daten, f, indent=2)
|
||||
|
||||
def teste_verbindung(db_config):
|
||||
try:
|
||||
conn = mysql.connector.connect(**db_config)
|
||||
conn.close()
|
||||
return True
|
||||
except mysql.connector.Error as e:
|
||||
print(f"[Fehler] DB-Verbindung fehlgeschlagen: {e}")
|
||||
return False
|
||||
|
||||
def initialisiere_admin_user(db_config):
|
||||
import mysql.connector
|
||||
conn = mysql.connector.connect(**db_config)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Tabelle erstellen, falls nicht vorhanden
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(255) UNIQUE NOT NULL,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role ENUM('user', 'admin') NOT NULL DEFAULT 'user'
|
||||
)
|
||||
""")
|
||||
|
||||
# Prüfen, ob admin existiert
|
||||
cursor.execute("SELECT id FROM users WHERE username = 'admin'")
|
||||
if not cursor.fetchone():
|
||||
cursor.execute("""
|
||||
INSERT INTO users (username, password, role)
|
||||
VALUES (%s, %s, 'admin')
|
||||
""", ('admin', 'admin'))
|
||||
print("[INFO] Admin-Account wurde erstellt: admin / admin")
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user