2024-09-02 09:02:32 +02:00
|
|
|
import sqlite3
|
2024-09-02 09:35:10 +02:00
|
|
|
import os
|
|
|
|
from flask import current_app
|
2024-09-02 10:45:58 +02:00
|
|
|
from werkzeug.security import generate_password_hash
|
2024-09-02 09:02:32 +02:00
|
|
|
|
|
|
|
def init_db():
|
2024-09-02 09:35:10 +02:00
|
|
|
db_path = os.path.join(current_app.instance_path, 'games.db')
|
|
|
|
conn = sqlite3.connect(db_path)
|
2024-09-02 09:02:32 +02:00
|
|
|
cursor = conn.cursor()
|
2024-09-02 09:31:38 +02:00
|
|
|
|
|
|
|
# Tabelle für Spiele erstellen
|
2024-09-02 09:02:32 +02:00
|
|
|
cursor.execute('''
|
|
|
|
CREATE TABLE IF NOT EXISTS games (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
image TEXT,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
date TEXT,
|
|
|
|
buyer TEXT,
|
|
|
|
owned BOOLEAN NOT NULL
|
|
|
|
)
|
|
|
|
''')
|
2024-09-02 09:31:38 +02:00
|
|
|
|
|
|
|
# Tabelle für Benutzer erstellen
|
|
|
|
cursor.execute('''
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
username TEXT NOT NULL UNIQUE,
|
2024-09-02 10:32:26 +02:00
|
|
|
password TEXT NOT NULL,
|
|
|
|
role TEXT NOT NULL CHECK(role IN ('user', 'admin')),
|
|
|
|
last_login TEXT
|
2024-09-02 09:31:38 +02:00
|
|
|
)
|
|
|
|
''')
|
|
|
|
|
2024-09-02 10:45:58 +02:00
|
|
|
# Standardbenutzer erstellen
|
|
|
|
hashed_password = generate_password_hash("admin")
|
|
|
|
cursor.execute('''
|
|
|
|
INSERT OR IGNORE INTO users (username, password, role)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
''', ("admin", hashed_password, "admin"))
|
|
|
|
|
2024-09-02 09:02:32 +02:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|