1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #!/usr/bin/python3
# -*- coding: utf-8 -*-
from db_connection import * #Database importation
class Employe:
"""Class with employe caracteritics and methods"""
def __init__(self):
"""Default constructor
Initialization of employe"""
self.cursor = db_connection.db.cursor()
self.nom = " "
self.prenom = " "
self.sexe = " "
self.date_n = 0
self.lieu_n = " "
def afficher(self):# Method to print all data from database
self.cursor.execute("SELECT * FROM employe")
data = self.cursor.fetchall()
for row in data:
print("Identifiant :{}, Nom :{}, Prénom :{}, Sexe :{}, Date de naissance :{}, Ville :{}".
format(row[0], row[1], row[2], row[3], row[4], row[5]))
"""db_connection.db.close()"""
def ajouter(self):# Method to add a employee into database
print("Veuillez entrer vos informations : ")
self.nom = input("Nom :")
self.prenom = input("Prenom :")
self.sexe = input("Sexe :")
self.date_n = "2015-05-15"
self.lieu_n = input("Lieu de naissance :")
req = "INSERT INTO employe (nom, prenom, sexe, date_nais, lieu_nais) VALUES(%s, %s, %s, %s, %s)"
self.arg = (self.nom, self.prenom, self.sexe, self.date_n, self.lieu_n)
self.cursor.execute(req, self.arg)
"""db_connection.db.close()"""
#Test
if __name__ == "__main__":
personne = Employe()
personne.afficher()# Jusqu'ici tout marche
personne1 = Employe() # Avant d'ajouter un employe, il faut d'abord le créer
personne1.ajouter() # Appel de méthode pour ajout des informations
personne1.afficher() " M'affiche que les information de cette ligne : personne.afficher() |
Partager