Files
elo-rezept-rechner/src/db.py

37 lines
921 B
Python

import csv
import psycopg2
# Pfad zur Ihrer CSV-Datei
csv_file_path = 'nu.csv'
# Pfad zur Ihrer SQLite-Datenbank
# Verbindung zur SQLite-Datenbank herstellen
conn = psycopg2.connect()
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 (%s, %s, %s, %s, %s, %s, %s)', row)
# Änderungen speichern und Verbindung schließen
conn.commit()
conn.close()