Commit 31be7b10 authored by Francois Mares's avatar Francois Mares
Browse files

Replace SQL.py

parent 16ad0bcc
......@@ -9,6 +9,8 @@ class SQL():
_PASSWORD = 'RdhPbg7H'
_USER = 'nf18p103'
_connexion=None
def __init__(self,HOST=None, DATABASE=None, PASSWORD=None, USER=None):
if(HOST):
self._HOST=HOST
......@@ -19,41 +21,62 @@ class SQL():
if(USER):
self._USER=USER
def __del__(self):
self._close_connexion()
# _PROTECTED
def _connexion(self):
# CONNEXION
def _new_connexion(self):
if self._connexion:
self._connexion.close()
print("Last connexion closed.")
self._connexion=None
try:
return psycopg2.connect(f"dbname='{self._DATABASE}' user ='{self._USER}' host='{self._HOST}' password='{self._PASSWORD}'")
self._connexion=psycopg2.connect(f"dbname='{self._DATABASE}' user ='{self._USER}' host='{self._HOST}' password='{self._PASSWORD}'")
print("Nouvelle connexion établie")
return self._connexion
except psycopg2.Error as e:
print("Message système :", e)
return None
def _verify_connexion_or_new(self):
if self._connexion:
return self._connexion
return self._new_connexion()
def _close_connexion(self):
if self._connexion:
self._connexion.close()
self._connexion=None
print("Connexion fermée.")
# SQL
def _execute_sql(self, file_path, mode='r'):
connexion = self._connexion()
if connexion:
self._verify_connexion_or_new()
if self._connexion:
try :
cur = connexion.cursor()
cur = self._connexion.cursor()
cur.execute(open(file_path, mode).read())
connexion.commit()
connexion.close()
self._connexion.commit()
return
except psycopg2.Error as e:
connexion.rollback()
print("Message système :", e)
self._connexion.rollback()
def _requete_sql(self,sql):
connexion = self._connexion()
if connexion:
self._verify_connexion_or_new()
if self._connexion:
try:
cur = connexion.cursor()
cur = self._connexion.cursor()
cur.execute(sql)
rows=cur.fetchall()
connexion.close()
return rows
except psycopg2.Error as e:
connexion.rollback()
print("Message système :", e)
self._connexion.rollback()
def _insert_into(self,class_name,values):
sql="INSERT INTO "+class_name+" VALUES("
......
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