Exercice 18
Sous les mêmes hypothèses que l’exercice précédent (Exercice 17), écrire un script permettant d’afficher sur une fenêtre tkinter, un formulaire d’insertion de données dans la table students et d’imprimer les données de la table students à l’écran :

ID : 1
name : ADAM
Email : adam@gmail.com
Age : 26
---------------------------
ID : 2
name : Elisa
Email : elisa@gmail.com
Age : 22
---------------------------
ID : 3
name : Majid
Email : majid@gmail.com
Age : 27
Solution
|
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# coding: utf-8 from tkinter import* import sqlite3 def validate(): # récupération des données du formulaire name = entryName.get() email = entryEmail.get() age = entryAge.get() conn = sqlite3.connect('mydatabase.db') cur = conn.cursor() req1 = "CREATE TABLE IF NOT EXISTS students(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL\ ,email TEXT NOT NULL , age INTEGER NOT NULL)" cur.execute(req1) req2 = "INSERT INTO students (name , email, age) values (?, ?, ?)" cur.execute(req2 , (name, email, age)) conn.commit() conn.close() root = Tk() root.geometry("600x400") #============================== # create a form to insert data #============================== # Label & Entry for name lblName = Label(root , text = "Name : ") lblName.place(x = 10 , y = 10) entryName = Entry(root ) entryName.place(x = 100 , y = 10 , width = 200) # Label & Entry Email lblEmail = Label(root , text = "Email") lblEmail.place( x = 10 , y = 40 ) entryEmail = Entry(root) entryEmail.place( x = 100 , y = 40 , width = 200) # Label & Entry Age lblAge = Label(root , text = "Age") lblAge.place( x = 10 , y = 70 ) entryAge = Entry(root) entryAge.place( x = 100 , y = 70 , width = 200) # Button Action btnValidate = Button(root , text = "Validate" , command = validate) btnValidate.place(x = 100 , y = 100, width = 200 , height = 25) #============== # Display data #============== conn = sqlite3.connect('mydatabase.db') cur = conn.cursor() result = cur.execute("select * from students") for row in result: print("ID : ", row[0]) print("Name : ", row[1]) print("Email : ", row[2]) print("Age : ", row[3]) print("--------------------------") root.mainloop() |
Younes Derfoufi
CRMEF OUJDA
Acheter sur Très Facile !
-

Maîtriser la Programmation Python pour le Machine Learning
€ 33,00 Acheter le livre -

Apprendre Python au le Lycée: Seconde, Première, Terminale
€ 14,00 Acheter le livre -
Promo !

Apprendre Python un guide complet et progressif
Le prix initial était : € 11,00.€ 10,00Le prix actuel est : € 10,00. Ajouter au panier
1 thought on “Solution Exercice 18: imprimer les données SQLite à l'écran”