From 43d98161b0a2768c511a0cf7bad3ccde72ef546f Mon Sep 17 00:00:00 2001 From: hichbra <hicham.brahimi@laposte.net> Date: Tue, 5 Mar 2019 11:01:41 +0100 Subject: [PATCH] fix fileToSql --- PC/FileToSql.py | 64 +++++++++++++++++++++++++++++++++---------------- PC/server.py | 2 +- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/PC/FileToSql.py b/PC/FileToSql.py index 1cd0f55..449ca79 100644 --- a/PC/FileToSql.py +++ b/PC/FileToSql.py @@ -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() diff --git a/PC/server.py b/PC/server.py index 23aa60e..83a8844 100644 --- a/PC/server.py +++ b/PC/server.py @@ -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: -- GitLab