Files
Random-Word-API/init.py
2025-11-19 14:13:31 +01:00

50 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()