-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.py
More file actions
37 lines (31 loc) · 1.08 KB
/
Copy pathinsert_data.py
File metadata and controls
37 lines (31 loc) · 1.08 KB
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
# insert_data.py
# Insere des donnees dans la table utilisateurs
import mysql.connector
from config import DB_CONFIG
def insert_data(nom=None, age=None):
"""Insere un utilisateur dans la table utilisateurs."""
connexion = None
cursor = None
try:
connexion = mysql.connector.connect(**DB_CONFIG)
cursor = connexion.cursor()
# Si nom et age sont fournis, les utiliser
if nom and age is not None:
requete = "INSERT INTO utilisateurs (nom, age) VALUES (%s, %s)"
valeurs = (nom, age)
else:
# Valeurs par defaut
requete = "INSERT INTO utilisateurs (nom, age) VALUES (%s, %s)"
valeurs = ("Alice", 25)
cursor.execute(requete, valeurs)
connexion.commit()
print("Donnees inserees avec succes !")
except mysql.connector.Error as erreur:
print(f"Erreur MySQL : {erreur}")
finally:
if cursor:
cursor.close()
if connexion and connexion.is_connected():
connexion.close()
if __name__ == "__main__":
insert_data()