mit Python csv in mysql einbinden
Verfasst: Donnerstag 8. August 2019, 08:56
Hallo,
ich bin Neuling in Sachen Python und probiere gerade den Inhalt einer csv-Datei in eine Datenbank ber Python-Script zu schreiben.
die Datenbank:
CREATE TABLE mytable(
time VARCHAR(19) NOT NULL PRIMARY KEY
,msg VARCHAR(30)
,codes VARCHAR(30)
,model VARCHAR(16) NOT NULL
,id INTEGER NOT NULL
,temperature_F NUMERIC(6,3) NOT NULL
,humidity INTEGER NOT NULL
);
die csv-Datei:
time,msg,codes,model,id,temperature_F,humidity
2019-08-08 08:14:55,,,inFactory sensor,142,59.300,82
2019-08-08 08:14:56,,,inFactory sensor,142,59.300,82
2019-08-08 08:14:56,,,inFactory sensor,142,59.300,82
das Python-Script:
#!/usr/bin/python
import sys
import mysql.connector
import csv
# Verbindung zur Datenbank
try:
connection = mysql.connector.connect (host = "localhost", user = "user", passwd = "password", db = "db")
except:
print ("Keine Verbindung zum Server")
sys.exit(0)
# Execution-Objekt erzeugen
cursor = connection.cursor()
# Daten aus csv
with open('r91.csv', "rt") as ifile:
read = csv.reader(ifile)
for row in read:
cursor.execute("INSERT INTO mytable(time,msg,codes,model,id,temperature_F,humidity) VALUES(?, ?, ?, ?, ?, ?, ?)", row)
connection.commit()
# Execution-Objekt schliessen
cursor.close()
# Verbindung schliessen
connection.close()
die Fehlermeldung:
Traceback (most recent call last):
File "r91_db.py", line 21, in <module>
cursor.execute("INSERT INTO mytable(time,msg,codes,model,id,temperature_F,humidity) VALUES(?, ?, ?, ?, ?, ?, ?)", row)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 558, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
ich hoffe mir kann jemand helfen.
Danke im Voraus.
ich bin Neuling in Sachen Python und probiere gerade den Inhalt einer csv-Datei in eine Datenbank ber Python-Script zu schreiben.
die Datenbank:
CREATE TABLE mytable(
time VARCHAR(19) NOT NULL PRIMARY KEY
,msg VARCHAR(30)
,codes VARCHAR(30)
,model VARCHAR(16) NOT NULL
,id INTEGER NOT NULL
,temperature_F NUMERIC(6,3) NOT NULL
,humidity INTEGER NOT NULL
);
die csv-Datei:
time,msg,codes,model,id,temperature_F,humidity
2019-08-08 08:14:55,,,inFactory sensor,142,59.300,82
2019-08-08 08:14:56,,,inFactory sensor,142,59.300,82
2019-08-08 08:14:56,,,inFactory sensor,142,59.300,82
das Python-Script:
#!/usr/bin/python
import sys
import mysql.connector
import csv
# Verbindung zur Datenbank
try:
connection = mysql.connector.connect (host = "localhost", user = "user", passwd = "password", db = "db")
except:
print ("Keine Verbindung zum Server")
sys.exit(0)
# Execution-Objekt erzeugen
cursor = connection.cursor()
# Daten aus csv
with open('r91.csv', "rt") as ifile:
read = csv.reader(ifile)
for row in read:
cursor.execute("INSERT INTO mytable(time,msg,codes,model,id,temperature_F,humidity) VALUES(?, ?, ?, ?, ?, ?, ?)", row)
connection.commit()
# Execution-Objekt schliessen
cursor.close()
# Verbindung schliessen
connection.close()
die Fehlermeldung:
Traceback (most recent call last):
File "r91_db.py", line 21, in <module>
cursor.execute("INSERT INTO mytable(time,msg,codes,model,id,temperature_F,humidity) VALUES(?, ?, ?, ?, ?, ?, ?)", row)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 558, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
ich hoffe mir kann jemand helfen.
Danke im Voraus.