Commit 596e87cf authored by Francois Mares's avatar Francois Mares
Browse files

Replace SQL.py

parent 67a288df
import psycopg2 import psycopg2
"""
Classe d'interface avec une base de données
"""
class SQL(): class SQL():
HOST = 'tuxa.sme.utc' _HOST = 'tuxa.sme.utc'
DATABASE = 'dbnf18p103' _DATABASE = 'dbnf18p103'
PASSWORD = 'RdhPbg7H' _PASSWORD = 'RdhPbg7H'
USER = 'nf18p103' _USER = 'nf18p103'
def __init__(self,HOST=None, DATABASE=None, PASSWORD=None, USER=None):
if(HOST):
self._HOST=HOST
if(DATABASE):
self._DATABASE=DATABASE
if(PASSWORD):
self._PASSWORD=PASSWORD
if(USER):
self._USER=USER
def _connexion(self):
try:
return psycopg2.connect(f"dbname='{self._DATABASE}' user ='{self._USER}' host='{self._HOST}' password='{self._PASSWORD}'")
except psycopg2.DatabaseError as e:
connexion.rollback()
print("Message système :", e)
# PUBLIC:
def requete_sql(self,sql): def requete_sql(self,sql):
connexion = self._connexion()
try: try:
connexion = psycopg2.connect(f"dbname='{DATABASE}' user ='{USER}' host='{HOST}' password='{PASSWORD}'")
cur = connexion.cursor() cur = connexion.cursor()
cur.execute(sql) cur.execute(sql)
rows=cur.fetchall() rows=cur.fetchall()
connexion.close() connexion.close()
return rows return rows
except psycopg2.IntegrityError as e: except psycopg2.Error as e:
connexion.rollback() connexion.rollback()
print("Message système :", e) print("Message système :", e)
def insert_into(self,classe,values): def insert_into(self,class_name,values):
sql="INSERT INTO "+classe+" VALUES("+values+")"
sql="INSERT INTO "+class_name+" VALUES("
for value in values:
sql+="'"+value+"',"
sql=sql[:-1]
sql+=")"
connexion = self._connexion()
try: try:
connexion = psycopg2.connect(f"dbname='{DATABASE}' user ='{USER}' host='{HOST}' password='{PASSWORD}'")
cur = connexion.cursor() cur = connexion.cursor()
cur.execute(sql) cur.execute(sql)
connexion.close() connexion.close()
except psycopg2.DataError as e: except psycopg2.DataError as e:
connexion.rollback() connexion.rollback()
print("Message système :", e) print("Message système :", e)
\ No newline at end of file
Supports Markdown
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