TP Prise en main SQL/Python #

Il existe deux manières de taper des lignes de commandes SQL dans un Jupyter Notebook :
avec des décorateurs pour taper directement des lignes de commandes sql
avec des queries
Avec les décorateurs#
%load_ext sql
%config SqlMagic.autocommit = True
%sql sqlite:///liste_eleve.sqlite
%sql
* sqlite:///liste_eleve.sqlite
%%sql
SELECT name FROM sqlite_master WHERE type='table';
* sqlite:///liste_eleve.sqlite
Done.
| name |
|---|
| liste_eleve |
%%sql
SELECT Nom FROM liste_eleve LIMIT 10;
* sqlite:///liste_eleve.sqlite
Done.
| Nom |
|---|
| ABBAD |
| ABD EL NABI |
| ABDELOUAHED |
| ABED |
| ABIDOGOUN |
| ABOUE |
| ABOULRAS |
| ABUNIAWAN |
| ACHIBANE |
| ADELAIDE |
Avec les Queries#
import sqlite3
import pandas as pd
# Connexion à la base (fichier dans le même dossier que le notebook)
conn = sqlite3.connect("liste_eleve.sqlite")
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print("Tables trouvées :")
for t in tables:
print("-", t[0])
Tables trouvées :
- liste_eleve
cursor.execute("SELECT * FROM liste_eleve LIMIT 5;")
rows = cursor.fetchall()
# Afficher les lignes
for row in rows:
print(row)
('ABBAD', 'Chaima', 'abbad.chaima@gmail.com', '6/4/2004', '0736061000', 'Féminin', '', '', 'Kiwi', 'Externe', 'Football', 'Magie', '2CAP2 COMMER.SERV.HOTEL-CAFE-RESTAURANT', '', 'ANGLAIS LV1', 'FAUX', '16,98')
('ABD EL NABI', 'Hazem', 'abd el nabi.hazem@outlook.com', '5/5/2002', '0775456577', 'Masculin', '', '', 'Poisson', 'Externe', 'Football', 'Théâtre', '2NDPRO METIERS ELECT. ENVIRON. CONNECTES', '', 'ANGLAIS LV1', 'FAUX', '11,24')
('ABDELOUAHED', 'Noureddine', 'abdelouahed.noureddine@gmail.com', '28/1/2004', '0699645198', 'Masculin', '', '', 'Poisson', 'Externe', 'Tennis', 'Théâtre', '', '', 'ANGLAIS LV1', 'FAUX', '16,04')
('ABED', 'Yannis', 'abed.yannis@orange.fr', '11/11/2000', '0621047027', 'Masculin', '', '', '', 'Demi-pensionnaire', 'Tennis', 'Natation', 'TLEPRO VENTE (PROSPECT.NEGO.SUIV.CLIENT)', 'Musculation Tle G1 R', 'ANGLAIS LV1, ESPAGNOL LV2', 'FAUX', '12,79')
('ABIDOGOUN', 'Alexandre', 'abidogoun.alexandre@free.fr', '3/9/2000', '0640677942', 'Masculin', '', '', 'Fruits à coque', 'Externe', 'Basketball', 'Peinture', '2NDPRO MET. RELATION CLIENT 2NDE COMMUNE', '2MCV1FRANCA, 2MCV1MATHS, 2MCV1VENTP', 'ANGLAIS LV1, ESPAGNOL LV2', 'FAUX', '17,95')
df = pd.read_sql_query("SELECT * FROM liste_eleve", conn)
# Affichage propre
pd.set_option('display.max_columns', None) # pour tout afficher
pd.set_option('display.max_rows', 10) # limite l'affichage si gros tableau
display(df.head()) # ou juste df
| Nom | Prénom | Né(e) le | Téléphone | Sexe | Classe | Projet d'accompagnement | Allergies | Régime | Sport | Club | Formation | Groupes | Toutes les options | Redoublement | Moyenne Générale | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | ABBAD | Chaima | abbad.chaima@gmail.com | 6/4/2004 | 0736061000 | Féminin | Kiwi | Externe | Football | Magie | 2CAP2 COMMER.SERV.HOTEL-CAFE-RESTAURANT | ANGLAIS LV1 | FAUX | 16,98 | |||
| 1 | ABD EL NABI | Hazem | abd el nabi.hazem@outlook.com | 5/5/2002 | 0775456577 | Masculin | Poisson | Externe | Football | Théâtre | 2NDPRO METIERS ELECT. ENVIRON. CONNECTES | ANGLAIS LV1 | FAUX | 11,24 | |||
| 2 | ABDELOUAHED | Noureddine | abdelouahed.noureddine@gmail.com | 28/1/2004 | 0699645198 | Masculin | Poisson | Externe | Tennis | Théâtre | ANGLAIS LV1 | FAUX | 16,04 | ||||
| 3 | ABED | Yannis | abed.yannis@orange.fr | 11/11/2000 | 0621047027 | Masculin | Demi-pensionnaire | Tennis | Natation | TLEPRO VENTE (PROSPECT.NEGO.SUIV.CLIENT) | Musculation Tle G1 R | ANGLAIS LV1, ESPAGNOL LV2 | FAUX | 12,79 | |||
| 4 | ABIDOGOUN | Alexandre | abidogoun.alexandre@free.fr | 3/9/2000 | 0640677942 | Masculin | Fruits à coque | Externe | Basketball | Peinture | 2NDPRO MET. RELATION CLIENT 2NDE COMMUNE | 2MCV1FRANCA, 2MCV1MATHS, 2MCV1VENTP | ANGLAIS LV1, ESPAGNOL LV2 | FAUX | 17,95 |
# Femer la connexion
conn.close()