Sunday, May 15, 2016

Python CGI Database (2 of 4)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlite3

print "Content-Type: application/xhtml+xml; charset=utf-8\n"

print '<!DOCTYPE html>'
print '<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">'
print '<head>'
print '    <title>eCarnet - Home</title>'
print '</head>'
print '<body>'
print '    <h1>Bienvenue sur eCarnet de ULB</h1>'
print '    <h2>Employés</h2>'

# Connection à la base de données
#db_connection = sqlite3.connect('database.sqlite3')
db_connection = sqlite3.connect('database.test-01.db')
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()

# Sélection des enregistrements
cursor.execute("SELECT prenom, nom, tel_fixe FROM employe")

# Création de la liste des employées
rows = cursor.fetchall()
print '    <ol>'
for row in rows:
    print'        <li>' +row['prenom'] + ' ' +row['nom'] + ', ' +row['tel_fixe'] + '  </li>'
print '    </ol>'
   
# Formulaire de recherche des employés d'un service   
print '    <h2>Employés par service</h2>'
print '    <form action="eCarnet_service.py" method="get">'
print '    <p><select name="service">'
cursor.execute ("SELECT id, nom, emplacement From service")
rows = cursor.fetchall()
for row in rows:
    print '            <option value="' + str(row['id']) + '">' + row['emplacement'] + '</option>'
print '    </select>'
print '        <input type="submit" value="Lister" /></p>'
print '    </form>'

# Formulaire d'ajout d'un nouvel employé   
print '    <h2>Ajouter un nouvel employé</h2>'
print '    <form action="eCarnet_add_employee.py" method="get">'
print '    <p>Prénom : <input type="text" name="prenom" /></p>'
print '    <p>Nom : <input type="text" name="nom" /></p>'
print '    <p>Matricule : <input type="text" name="matricule" /></p>'
print '    <p>Tèl. fixe : <input type="text" name="tel_fixe" /></p>'
print '    <p>Service : <select name="service">'
for row in rows:
    print '            <option value="' + str(row['id']) + '">' + row['emplacement'] + '</option>'
print '        </select>'
print '        <input type="submit" value="Ajouter" /></p>'
print '    </form>'  
print '</body>'
print '</html>'

db_connection.close()

No comments:

Post a Comment