Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active March 24, 2016 18:18
Show Gist options
  • Save lovasoa/c9c592b7ef15231a15af to your computer and use it in GitHub Desktop.
Save lovasoa/c9c592b7ef15231a15af to your computer and use it in GitHub Desktop.
import sqlite3
fichierExo1="test.sq3"
conn=sqlite3.connect(fichierExo1)
cur=conn.cursor()
cur.execute("CREATE TABLE compo (comp TEXT,a_naiss INTEGER, a_mort INTEGER)")
cur.execute("CREATE TABLE oeuvres2 (comp TEXT, titre TEXT, duree INTEGER, interpr TEXT)")
data=[("Mozart",1756,1791),("Beethoven",1770,1827),("Haendel",1685,1759),("Schubert",1797,1828),("Chopin",1810,1849),("Vivaldi",1678,1741),("Monteverdi",1567,1643),
("Bach",1685,1750),("Shostakovich",1906,1975)]
for i in data: #parcours des tuples
cur.execute("INSERT INTO compo(comp, a_naiss, a_mort) VALUES(?,?,?)",i)
print(i)
data1=[("Vivaldi", "les quatre saisons", 20, "T.Pinnock"),("Mozart", "Concerto piano n°12", 25, "M.Perahia"),("Brahms", "Concerto violon n°2", 40, "A.grumiaux"),("Beethoven", "Sonate 'au clair de lune' ", 14, " W. Kempf"),("Beethoven", "Sonate 'pathétique'", 17, " W. Kempf"),
("Schubert", "Quintette 'la truite' ", 39, "SE of London"),("Haydn","La création",109,"H. Von Karajan"),("Chopin","Concerto piano N°1",42,"M.J. Pires"),("Bach","Toccata & fugue",9,"P. Burmester"),
("Beethoven","Concerto piano N°4",33,"M. Pollini"),("Mozart","Symphonie N°40",29,"F. Bruggen"),("Mozart","Concerto piano N°22",35,"S. Richter"),("Beethoven","Concerto piano N°3",37,"S. Richter")]
for j in data1:
cur.execute("INSERT INTO oeuvres2(comp, titre, duree, interpr) VALUES(?,?,?,?)",j)
print(j)
while 1:
print("Veuillez entrer votre requête SQL (ou <Enter> pour terminer) :")
requete = input()
if requete =="":
break
try: #le try/except permet, si il y a une erreur dans le requete, de ne pas planter
cur.execute(requete) #requête SQL
except:
print('*** Requête SQL incorrecte ***')
else:
for enreg in cur: #affichage du résultat
print(enreg)
print()
choix=input("Confirmez-vous l'enregistrement de l'état actuel (o/n) ? ")
if choix[0]=="o" or choix[0]=="O":
conn.commit()
else:
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment