init script
This commit is contained in:
50
init.py
Normal file
50
init.py
Normal 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()
|
||||
Reference in New Issue
Block a user