ich bin neu hier und in Python noch ziemlich unerfahren, deswegen möchte ich mich für eventuelle banale Fragen schon im Voraus entschuldigen.
Ich möchte aus einer Liste eine Datenbank erstellen und habe dazu folgendes Tutorial gefunden und auf mein Programm angewendet:
https://python-kurs.eu/sql_python.php
Der relevante Programmausschnitt sieht folgendermaßen aus:
Code: Alles auswählen
import sqlite3
connection = sqlite3.connect("Bienen_Daten.db")
cursor = connection.cursor()
#delete
cursor.execute("""DROP TABLE Bienen;""")
#folgendes nur einmal(nicht in Schleife einbauen)
#namen kontrollieren
sql_command = """
CREATE TABLE Bienen (
Datum_Datenbank DATE PRIMARY KEY,
temp_mean1 FLOAT,
temp_mean2 FLOAT,
tmax FLOAT,
tmin FLOAT,
Bienen_Anzahl INTEGER,
gewicht FLOAT);"""
cursor.execute(sql_command)
for p in liste_datenbank:
format_str = """INSERT INTO Bienen (Datum_Datenbank, temp_mean1, temp_mean2, tmax, tmin, Bienen_Anzahl,gewicht)
VALUES ("{datum_}", "{temp_mean1}", "{temp_mean2}", "{tmax_}","{tmin_}","{Bienen_Anzahl_}","{gewicht_}");"""
sql_command = format_str.format(datum_= p[0], temp_mean1=p[1], temp_mean2=p[2], tmax_ = p[3], tmin_ = p[4], Bienen_Anzahl_ = p[5], gewicht_ = p[6])
cursor.execute(sql_command)
cursor.execute(sql_command)
connection.commit()
connection.close()
Traceback (most recent call last):
File "/home/pi/010+001mit waage.py", line 228, in <module>
sql_command = format_str.format(datum_= p[0], temp_mean1=p[1], temp_mean2=p[2], tmax_ = p[3], tmin_ = p[4], Bienen_Anzahl_ = p[5], gewicht_ = p[6])
TypeError: 'float' object is not subscriptable
Kann mir damit jemand weiterhelfen?