Skip to content
Snippets Groups Projects
Commit 43d98161 authored by hichbra's avatar hichbra
Browse files

fix fileToSql

parent 39c7be9c
Branches
No related merge requests found
......@@ -3,26 +3,43 @@ import time
import sys
import os
import csv
import shutil
from datetime import datetime
db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan",
user="firediag",
passwd="firediag$!",
db="firediag");
log = open("logBDD.txt", "w")
nomCapteur = "Moule A"
try:
db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan",
user="firediag",
passwd="firediag$!",
db="firediag");
except Exception as ex:
log.write(str(datetime.now())+" ==> Exception Demarrage : "+str(type(ex))+" ("+str(ex.args)+")\n")
print(ex);
log.close()
sys.exit(1)
nomCapteur = "Moule A" # A0 / A1 / A2 / A3 / A4 / A5
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])
try:
#--- 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])
except Exception as ex:
log.write(str(datetime.now())+" ==> Exception Init : "+str(type(ex))+" ("+str(ex.args)+")\n")
print(ex);
log.close()
sys.exit(1)
print(tabId)
FOLDER = "./data/"
TRAITEES = "./traitees/"
DIR = os.listdir(FOLDER)
DIR.sort()
......@@ -30,9 +47,9 @@ 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:
print(FOLDER+f)
with open(FOLDER+f,'r') as csvfile:
try:
#--- Recupere la source du fichier
id = csvfile.readline().strip().split(":")[1]
print(id)
......@@ -47,9 +64,14 @@ for f in DIR:
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);
except Exception as ex:
db.rollback();
log.write(str(datetime.now())+" ==> Exception Requete : "+str(type(ex))+" ("+str(ex.args)+")\n")
print("rollback ");
print(ex);
db.commit();
#os.remove(FOLDER+f)
shutil.move(FOLDER+f, TRAITEES+f)
db.close();
log.close()
......@@ -6,7 +6,7 @@ from datetime import datetime
CHUNK_SIZE = 2048
PORT = 20000
log = open("log.txt", "w")
log = open("logServer.txt", "w")
file = None
while True:
......
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