Sunday, May 15, 2016

Python CGI Database (3 of 4)

# -*- coding: utf-8 -*-

import sqlite3
import cgi

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 - Employées d un service</title>'
print '</head>'
print '<body>'

form = cgi.FieldStorage()
service_id = str(form["service"].value)
db_connection = sqlite3.connect('database.test-01.db')
db_connection.row_factory = sqlite3.Row
cursor = db_connection.cursor()
cursor.execute("SELECT emplacement FROM service WHERE id=" + service_id)
row = cursor.fetchone()
service_nom = str(row['emplacement'])

print '    <h1>Employés du service - ' + service_nom + ' -</h1>'

cursor.execute("SELECT prenom, nom, tel_fixe FROM employe WHERE id_service=" + service_id)
rows = cursor.fetchall()

print '    <ol>'
for row in rows:
    print '        <li>' + row['prenom'] + ', ' +row['nom'] + ', ' + row['tel_fixe'] + '</li>'
print '    </ol>'
print '</body>'
print '</html>'

db_connection.close()

No comments:

Post a Comment