Search code examples
pythondatabaseberkeley-dbbsddb

Inserting data through form fields into the Berkeleys db with python


I have a form with FirstName, LastName, Age and Gender. Now How do i insert data into the Berkeley db with python ? I am really new to this kinda database. Most database have the sql queries to associate and a cursor to get and fetch data. But in case of Berkeley there is no such.

I have reading about Berkeleys db, but i am not getting it. Any ones help is appreciated

I am using Python 2.5

How can i Integrate this data which comming from form into Berkeley db ??

Theres some error comming when i run on server:

File "/usr/lib/python2.5/bsddb/__init__.py", line 306, in hashopen, referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181]     , referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] d.open(file, db.DB_HASH,     flags, mode), referer: http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] bsddb.db, referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] ., referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] DBAccessError, referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] : , referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] (13, 'Permission denied'),     referer: http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] , referer:     http://192.168.2.181/~jeremy/index.html
[Fri Nov 08 17:26:55 2013] [error] [client 192.168.2.181] Premature end of script     headers: database.py, referer: http://192.168.2.181/~jeremy/index.html

Code:

#!/usr/bin/python
import bsddb
import cgi

form = cgi.FieldStorage()

Fname = form.getvalue('firdtname')
Lname = form.getvalue('lastname')
Age = form.getvalue('age')
Gender = form.getvalue('gender')

db = bsddb.hashopen("/tmp/mydb.db","c")
db['FirstName'] = Fname
db['Lastname'] = Lname
db['Age'] = Age
db['Gender'] = Gender

db = bsddb.hashopen("/tmp/mydb.db")

print db['FirstName'], db['Lastname'], db['Age'], db['Gender']

db.close()

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

Solution

  • You read and write to it exactly like a dictionary.

    >>> import bsddb
    >>> db = bsddb.hashopen('mydb.db')
    >>> db['FirstName'] = 'Joe'
    >>> db['LastName'] = 'Doe'
    >>> db['Age'] = '30'
    >>> db.close()
    >>> 
    >>> db = bsddb.hashopen('mydb.db')
    >>> db['FirstName']
    'Joe'
    

    However, Berkeley DB stores only pairs of key/value strings, so maybe that's not what you really need if you want to store those values for several different entries. If no other language will be using that db file, maybe you can use the shelve module to store pickled dicts. If you need it to be easy for others to use, you could serialize your form data as json. Something like this:

    >>> import json
    >>> import bsddb
    >>> db = bsddb.hashopen('mydb.db')
    >>> form = {'FirstName': 'Joe', 'LastName': 'Doe', 'Age': 30}
    >>> db['joedoe'] = json.dumps(form)
    >>> db.close()
    >>> 
    >>> db = bsddb.hashopen('mydb.db')
    >>> json.loads(db['joedoe'])
    {'FirstName': 'Joe', 'LastName': 'Doe', 'Age': 30}
    

    But frankly, this starts to look more and more like an anti-pattern, and unless you're absolutely restricted to using Berkeley DB for some reason, you shouldn't be doing it that way. You should be using sqlite for that.