0

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"
Jeremy
  • 73
  • 1
  • 3
  • 10

2 Answers2

1

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

LotusUNSW
  • 2,027
  • 18
  • 20
  • Aww. I want in the pythonic way brother – Jeremy Nov 08 '13 at 09:33
  • @user2968229 As the link Ignacio showed, it's deprecated. This link https://pypi.python.org/pypi/bsddb3/ provides the Python Interface for Berk DB. – LotusUNSW Nov 08 '13 at 09:39
  • I have python 2.5.1 version installed. so bsddb3 wont be imported – Jeremy Nov 08 '13 at 09:41
  • This should help. Python 2.5.1 does > not come with any bsddb version whatsoever https://mail.python.org/pipermail/python-list/2007-November/456439.html – LotusUNSW Nov 08 '13 at 09:44
  • http://docs.python.org/2/library/bsddb.html#module-bsddb - This link code seems to be working fine in my python version – Jeremy Nov 08 '13 at 09:49
1

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.

Pedro Werneck
  • 40,902
  • 7
  • 64
  • 85
  • I have done this prograaming using a text file, where user can add all there data into the text file. Now i want the same i.e. the data to be integrated into the berkeley db. – Jeremy Nov 08 '13 at 09:46
  • How do i achieve the same using Berkeley db ? – Jeremy Nov 08 '13 at 09:47
  • That example is using Berkeley DB. mydb.db is a hash db file. – Pedro Werneck Nov 08 '13 at 09:55
  • What if I have multiple data records. Using this i wont be able to store data line by line and display the whole record of a person – Jeremy Nov 08 '13 at 10:06
  • Hence my last phrase, Berkeley DB is probably not what you need. You can store pickled dictionaries as values, but you'd be better using the shelve module, which already uses Berkely DB as engine. – Pedro Werneck Nov 08 '13 at 10:17
  • Look i have updated my code. I want that all to integrate into the Berkeley db ? how can i do that ? If you say using Shelve module, could you please show some snippet to how do i do it ? – Jeremy Nov 08 '13 at 10:26
  • I have been instructed to use only Berkeley db. Take the values from the form and insert it into Berkeleys db, so i am really confused to how should i do that ? or to where to i start from ?" – Jeremy Nov 08 '13 at 10:43
  • Also here the data in the code hardcore. the data should dynamically come from the form . – Jeremy Nov 08 '13 at 10:47
  • This is starting to look more and more like homework assignment. The above information should be more than enough for you to figure it out. Have fun. – Pedro Werneck Nov 08 '13 at 11:02
  • Its not an homework. i have been been doing myself with an some help from my senior – Jeremy Nov 08 '13 at 11:14
  • 1
    If it's not homework, you're doing it wrong. You shouldn't use Berkeley DB for that. – Pedro Werneck Nov 08 '13 at 11:25
  • I am using Berkeley db b'coz i dnt have any other database installed in my linux oS. Your help would be appreciated if you wish – Jeremy Nov 08 '13 at 11:33
  • Updated an Berkely db updated code with error_log. wahts the problem ? – Jeremy Nov 08 '13 at 12:08