I have a html form which has Firstname
, LastName
, Age
and Gender
and a ADD
button.
I enter the data into the form and that gets into the Berkeelys db. What my code does is it prints only the last values. I want that it should show all the values related to particular key
#!/usr/bin/python
import bsddb
import cgi
form = cgi.FieldStorage()
print "Content-type:text/html\n"
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')
#print Fname, Lname, Age
db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db","w")
db['FirstName'] = Fname
db['LastName'] = Lname
db['Age'] = Age
db['Gender'] = Gender
db.close()
db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db","r")
#db = bsddb.hashopen("/home/neeraj/public_html/database/mydb.db")
print db['FirstName'], db['LastName'], db['Age'], db['Gender']
db.close()
print db
You should use an SQL database instead of the
dict
-based interface, since SQL databases already handle multiple tuples in a table.Anyway, if you want to have a
dict
interface you can use theshelve
module (bsddb
is deprecated, so you should avoid it) and save each value in alist
:The above code outputs:
If you want to use an SQL database you could use the
sqlite3
module. For example:The above code outputs:
(Note the
u'...'
simply means that the strings are unicode, it doesn't change their value)However this code has some problems (e.g. try to run it twice...), but if you want to follow this path then you must learn SQL first, so go ahead and stufy it (there are a lot of online tutorials. For example
w3schools
ones).