[Tutor] show all dbm data

federico ramirez fedekiller at gmail.com
Thu Sep 14 20:43:19 CEST 2006


Hi, im have just started programming python with cgi and i will try to use
dbm as a db to practise
But i cant figure out how to diplay all the data from it..

this is the code im using

/////////////
#!/usr/bin/python

print "Content-Type: text/html\n\n"

import cgi, dbm

def startpage(title):
    print '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
    <title>''',title,'''</title>
    </head>
    <body>'''

def endpage():
    print """</body>
    </html>"""

def main():
    form = cgi.FieldStorage()
    if(form.has_key("name") and form["name"].value != ""):
        try:
            db = dbm.open("dbm", "c")
            db["name"] = form["name"].value
            db.close()
            print "Info saved. Now, <a href='?read=true'>read</a> it?"
        except:
            print cgi.print_exceptions()
    elif(form.has_key("read") and form["read"].value == "true"):
        try:
            db = dbm.open("dbm", "r")
            for key in db.keys():
                print key
        except:
            print cgi.print_exceptions()
    else:
        print """<form action="test.py" method="post">
        <input name="name" type="text" id="name" />
        <input type="submit" name="Submit" value="Send my name" />
        </form>"""

startpage("Test Page")
main()
endpage()
/////////////
Thanks in advance

-- 
Best Regards.
fedekiller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060914/8e5f5a8b/attachment.html 


More information about the Tutor mailing list