Commit 5209c37a authored by hichbra's avatar hichbra

File to Sql

parent d6fe421e
import MySQLdb
import time
import sys
import os
import csv
db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan",
user="firediag",
passwd="firediag$!",
db="firediag");
nomCapteur = "Moule A"
tabId = {}
#--- Recupere les id des capteurs en base de donnees
for i in range(0,6):
requete = "SELECT DISTINCT id FROM capteur WHERE nom = '"+nomCapteur+""+str(i)+"'"
cursor = db.cursor();
cursor.execute(requete)
idTab = cursor.fetchall()
tabId["A"+str(i)] = int(idTab[0][0])
print(tabId)
FOLDER = "./Target/"
DIR = os.listdir(FOLDER)
DIR.sort()
cursor = db.cursor();
#--- Parcours les fichiers, ajoute les valeurs en base et supprime le fichier
for f in DIR:
try:
print(FOLDER+f)
with open(FOLDER+f,'r') as csvfile:
#--- Recupere la source du fichier
id = csvfile.readline().strip().split(":")[1]
print(id)
next(csvfile, None)
valeurs = csv.reader(csvfile, delimiter=';')
for row in valeurs:
time = str(row[0]).strip()
for i in range(0,6):
requete = "INSERT INTO `valeur` (`temps`, `valeur`, `idCapteur`) VALUES ('"+str(time).strip()+"', '"+str(row[i+1]).strip()+"', '"+str(tabId["A"+str(i)])+"')"
cursor.execute(requete)
#print(row)
db.commit();
os.remove(FOLDER+f)
except Exception as e:
db.rollback();
print("rollback ");
print(e);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment