Question

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"
Was it helpful?

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.

OTHER TIPS

From the docs:

Python Docs Berkeley- see 'put'

#include <db.h>

int
DB->put(DB *db,
    DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);

The DB->put() method stores key/data pairs in the database. The default behavior of the DB->put() function is to enter the new key/data pair, replacing any previously existing key if duplicates are disallowed, or adding a duplicate data item if duplicates are allowed. If the database supports duplicates, the DB->put() method adds the new data value at the end of the duplicate set. If the database supports sorted duplicates, the new data value is inserted at the correct sorted location.

Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top