#!/usr/bin/python

import cgitb; cgitb.enable();
import sys
import cgi
import psycopg

dsn = "dbname=DBTEST host=localhost port=5432 user=postgres password=tuclave"

con = psycopg.connect(dsn)

cur = con.cursor()

s = "SELECT * FROM tb_usuarios"

# Dile al navegador el tipo de contenido
print "Content-Type: text/html\n\n"

# Pinta la cabeza html
print """<HTML><HEAD>
<TITLE>Python con Postgresql</TITLE>
</HEAD><BODY>"""

print "<table with='100%' align='center'>"

PYGS = "Hola Python y Postgresql!! (psycopg)"

print "<tr><td colspan='4'> Saludos " + PYGS + "</td></tr>" # pinta un saludo ñoño

form=cgi.FieldStorage() #preparo la toma de variables de ambiente get o post

if not form.getvalue("b"):
    valb = "no hay valor"
else:
  valb = form.getvalue("b")

if not form.getvalue("a"):
 vala = "no hay valor"
else:
 vala = form.getvalue("a")

print "%s -- %s" % (vala, valb)

cur.execute(s)

r = cur.fetchone()

while r != None:

   ID = int(r[0])
   nombre = str(r[1])
   email = str(r[2])

   # print cada renglón
   print "<tr><td><a href='editar.py?id=%s&action=1'>Editar</a></td><td> %s | %s </td><td><a href='editar.py?id=%s&action=2'>Borrar</a></td></tr>" % (ID, nombre, email, ID)
   
   r = cur.fetchone()


print "</table>"

#limpio el cursor

cur.close()

# cierro la conexion
con.close()

# termina SQL.

print "</BODY></HTML>" # termina html

# fin del archivo pgsql.py 
