Commit 92f5460b authored by hichbra's avatar hichbra

Update fileTosql

parent ac7582b6
......@@ -30,26 +30,22 @@ def formatTopicCorrect(topic):
except:
return False
log = open("logBDD.txt", "w")
# ================ CONNECTION BDD ================== #
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)
# ================ RECUPERATION TOPIC ================= #
topics = []
try:
def initBDD():
try:
db = MySQLdb.connect(host="localhost",#"pil-09.univlehavre.lan",
user="firediag",
passwd="firediag$!",
db="firediag");
return db
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)
def recuperationTopic(db):
topics = []
#--- Recupere les topic des capteurs en base de donnees
requete = "SELECT DISTINCT topic FROM capteur"
cursor = db.cursor();
......@@ -58,20 +54,12 @@ try:
# On filtre les mauvais topics (qui ne peuvent pas etre split par /)
if formatTopicCorrect(row[0]):
topics.append(row[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("Topics : ", topics)
return topics
# ================ RECUPERATION ID ================= #
def recuperationId(db):
tabId = {}
tabId = {}
try:
#--- Recupere les id des capteurs en fonction des topics
for topic in topics:
requete = "SELECT DISTINCT id FROM capteur WHERE topic = '"+topic+"'"
......@@ -79,22 +67,39 @@ try:
cursor.execute(requete)
idTab = cursor.fetchall()
tabId[topic] = 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("Id : ",tabId)
return tabId
log = open("logBDD.txt", "w")
FOLDER = "./data/"
TRAITEES = "./traitees/"
# ================ CONNECTION BDD ================== #
db = initBDD()
# ================ File ==> SQL ================= #
while(True):
FOLDER = "./data/"
TRAITEES = "./traitees/"
topics = []
tabId = {}
DIR = os.listdir(FOLDER)
DIR.sort()
try :
# ================ RECUPERATION TOPIC ================= #
topics = recuperationTopic(db)
# ================ RECUPERATION ID ================= #
tabId = recuperationId(db)
print("Id : ",tabId)
except:
log.write(str(datetime.now())+" ==> Exception Init : "+str(type(ex))+" ("+str(ex.args)+")\n")
print(ex);
db = initBDD()
continue
cursor = db.cursor();
#--- Parcours les fichiers, ajoute les valeurs en base et supprime le fichier
......@@ -129,7 +134,10 @@ while(True):
sys.exit(0)
except Exception as ex:
db.rollback();
try:
db.rollback();
except:
db = initBDD()
log.write(str(datetime.now())+" ==> Exception Requete : "+str(type(ex))+" ("+str(ex.args)+")\n")
print("rollback ");
print(ex);
......
......@@ -53,3 +53,4 @@ while True:
except Exception as ex:
print("Exception ", type(ex))
log.write(str(datetime.now())+" ==> Exception "+str(type(ex))+" ("+str(ex.args)+")\n")
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