containerized
This commit is contained in:
36
src/db.py
Normal file
36
src/db.py
Normal file
@ -0,0 +1,36 @@
|
||||
import csv
|
||||
import sqlite3
|
||||
|
||||
# Pfad zur Ihrer CSV-Datei
|
||||
csv_file_path = 'nu.csv'
|
||||
|
||||
# Pfad zur Ihrer SQLite-Datenbank
|
||||
sqlite_db_path = 'nutrition.db'
|
||||
|
||||
# Verbindung zur SQLite-Datenbank herstellen
|
||||
conn = sqlite3.connect(sqlite_db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Erstellen der Tabelle (falls noch nicht vorhanden)
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS nutrition_table (
|
||||
name TEXT,
|
||||
kcal REAL,
|
||||
EW REAL,
|
||||
Fett REAL,
|
||||
KH REAL,
|
||||
BST REAL,
|
||||
Ca REAL
|
||||
)
|
||||
''')
|
||||
|
||||
# Öffnen der CSV-Datei und Einfügen der Daten in die Datenbank
|
||||
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
next(reader, None) # Überspringen der Kopfzeile
|
||||
for row in reader:
|
||||
cursor.execute('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, Ca) VALUES (?, ?, ?, ?, ?, ?, ?)', row)
|
||||
|
||||
# Änderungen speichern und Verbindung schließen
|
||||
conn.commit()
|
||||
conn.close()
|
Reference in New Issue
Block a user