Skip to content
Snippets Groups Projects
Commit 92f5460b authored by hichbra's avatar hichbra
Browse files

Update fileTosql

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