Files
elo-rezept-rechner/src/db.py
Wolfgang Hottgenroth 3852d78725
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
table adjusted
2024-01-31 13:43:21 +01:00

40 lines
1.0 KiB
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 (
id serial not null,
name TEXT not null,
kcal REAL,
EW REAL,
Fett REAL,
KH REAL,
BST REAL,
Ca REAL,
constraint nutrition_table_pk primary key (id),
constraint nutrition_table_name_uk unique (name)
)
''')
# Ö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()