0% found this document useful (0 votes)
5 views2 pages

Requetes SQL_Python

The document provides a guide on how to connect Python to a MySQL database using the mysql-connector-python module. It includes code examples for inserting, displaying, deleting, and modifying records in a MySQL database named 'gestionetudiant'. Each operation is encapsulated in a try-except block to handle potential errors and ensure proper closure of the database connection.

Uploaded by

hernestemabiala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Requetes SQL_Python

The document provides a guide on how to connect Python to a MySQL database using the mysql-connector-python module. It includes code examples for inserting, displaying, deleting, and modifying records in a MySQL database named 'gestionetudiant'. Each operation is encapsulated in a try-except block to handle potential errors and ensure proper closure of the database connection.

Uploaded by

hernestemabiala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

MySQL et python

Application+BDD(Mysql)

1. connection Mysql avec python


pour connecter on aura besoin du Module: mysql-connector-python
pip install NomMOdule
2.Enregistrer de python vers Mysql
3.Modifier de python vers MySQL
4. Supprimer de python vers MySQL
5. Afficher(Rechercher) info dans Mysql

ENREGISTREMENT

import mysql.connector as mc
try:
conn = mc.connect( host = 'localhost', database = 'gestionetudiant', user =
'root', password = '')
cursor = conn.cursor()
req = 'insert into etudiant
values("kamuania","muamba","perside","0821831129","f","l2")'
cursor.execute(req)
except mc.Error as err:
print(err)
finally:
if(conn.is_connected()):
cursor.close()
conn.close()

AFFICHAGE

import mysql.connector as mc
try:
conn = mc.connect( host = 'localhost', database = 'gestionetudiant', user =
'root', password = '')
cursor = conn.cursor()
req = 'select * from etudiant'
cursor.execute(req)
info= cursor.fetchall()
for a in info:
print(a)
except mc.Error as err:
print(err)
finally:
if(conn.is_connected()):
cursor.close()
conn.close()

SUPPRESSION

import mysql.connector as mc
try:
conn = mc.connect( host = 'localhost', database = 'gestionetudiant', user =
'root', password = '')
cursor = conn.cursor()
req = 'delete from etudiant where nom="kamuania"'
cursor.execute(req)
except mc.Error as err:
print(err)
finally:
if(conn.is_connected()):
cursor.close()
conn.close()

MODFICATION
import mysql.connector as mc
try:
conn = mc.connect( host = 'localhost', database = 'gestionetudiant', user =
'root', password = '')
cursor = conn.cursor()
req = 'update etudiant set nom="Kam" where nom="SETI"'
cursor.execute(req)
except mc.Error as err:
print(err)
finally:
if(conn.is_connected()):
cursor.close()
conn.close()

You might also like