import pandas as pd
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
from Mysql_connect import Mysql_connect


class Bibliotheque :    

        
    id_bibliotheque=0
    connect = Mysql_connect()
        
    def __init__(self,id_bibliotheque):
        self.id_bibliotheque = id_bibliotheque

    def getId_bibliotheque(self):
        return self.id_bibliotheque
    
    def setId_bibliotheque (self, id_bibliotheque):
        self.id_bibliotheque = id_bibliotheque
    
    def creer_bibliotheque(self,id_bibliotheque):
        try :
  
            cnx = self.connect.connexion()
            
            if (cnx.is_connected()):
                print('is connected')
            
            cursor = cnx.cursor()
        
            
            requete = ('insert into bibliotheque (id_biblio) values (%s);')        
            values = (id_bibliotheque)
            print(requete,values)
            cursor.execute(requete,values)         

        
        
            cnx.commit()
            cursor.close()
            
            
            cnx.close()

        except mysql.connector.Error as error:
            print("Failed to insert record {}".format(error))

        
    
    def modifier_bibliotheque(self,id,new_id):
        try :
  
            cnx = self.connect.connexion()
            
            if (cnx.is_connected()):
                print('is connected')
            
            cursor = cnx.cursor()
        
            
            requete = ('update bibliotheque set id_biblio = %s where id_biblio = %s;')        
            values = (new_id,id)
            print(requete,values)
            cursor.execute(requete,values)         

            cnx.commit()
            cursor.close()
           
            cnx.close()

        except mysql.connector.Error as error:
            print("Failed to update record {}".format(error))

        
    
    def supprimer_bibliotheque(self,id):
        try :
  
            cnx = self.connect.connexion()
            
            if (cnx.is_connected()):
                print('is connected')
            
            cursor = cnx.cursor()
        
            
            requete = ('delete from bibliotheque where id_biblio = %s;')        
            values = (id)
            print(requete,values)
            cursor.execute(requete,values)         

            cnx.commit()
            cursor.close()
           
            cnx.close()

        except mysql.connector.Error as error:
            print("Failed to delete record {}".format(error))

        
    
    def liste_bibliotheques (self) :
        
        try :
  
            cnx = self.connect.connexion()
            
            if (cnx.is_connected()):
                print('is connected')

            cursor = cnx.cursor()

            cursor.execute('select * from bibliotheque;')

            
            row = cursor.fetchall()
            result = pd.DataFrame(row)

            print(result)
            
            cursor.close()  
            cnx.close()
        
        except mysql.connector.Error as error:
            print("Failed to select record {}".format(error))

        