init script

This commit is contained in:
2025-11-19 14:13:31 +01:00
parent 964cf3ba36
commit 37202aa2aa
2 changed files with 79 additions and 32 deletions

50
init.py Normal file
View File

@@ -0,0 +1,50 @@
import sqlite3
# 📁 Dateiname der Datenbank
DB_NAME = "hangman.db"
# 📝 Die Wortliste
INITIAL_WORDS = [
"Haus", "Tisch", "Apfel", "Straße", "Flugzeug",
"Schlüssel", "Computer", "Bibliothek", "Deutschland",
"Flasche", "Maus", "Wolke", "Lampe", "Spiegel",
"Kaffee", "Programmierung", "RestAPI"
]
def init_db():
"""Erstellt die Tabelle und füllt sie mit Startdaten."""
print(f"🔌 Verbinde zu {DB_NAME}...")
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Tabelle erstellen
cursor.execute('''
CREATE TABLE IF NOT EXISTS words (
id INTEGER PRIMARY KEY AUTOINCREMENT,
word TEXT NOT NULL UNIQUE
)
''')
# Prüfen, ob Daten da sind
cursor.execute('SELECT count(*) FROM words')
count = cursor.fetchone()[0]
if count == 0:
print("📥 Datenbank leer. Fülle mit Startdaten...")
inserted_count = 0
for word in INITIAL_WORDS:
try:
cursor.execute('INSERT INTO words (word) VALUES (?)', (word,))
inserted_count += 1
except sqlite3.IntegrityError:
pass
conn.commit()
print(f"{inserted_count} Wörter erfolgreich hinzugefügt.")
else:
print(f" Datenbank enthält bereits {count} Wörter. Keine Änderungen vorgenommen.")
conn.close()
print("🏁 Initialisierung abgeschlossen.")
if __name__ == "__main__":
init_db()