[pybsddb] Secondary Indices with bsddb3
denis.papathanasiou at gmail.com
denis.papathanasiou at gmail.com
Mon Nov 17 23:17:06 CET 2008
I'm having trouble getting the example from the C API
(http://www.oracle.com/technology/documentation/berkeley-db/db/ref/am/second.html)
to work in Python/bsddb3.
I'll describe the specifics of what I tried, below, but basically I
don't understand how to code the callback object in Python for the
DB.associate() method.
I started by creating the primary and secondary databases:
> from bsddb3 import db # the Berkeley db data base
>
> dbp = db.DB()
> dbp.open("students.db", None, db.DB_BTREE, db.DB_CREATE)
>
> sdbp = db.DB()
> sdbp.open("lastname.db", None, db.DB_BTREE, db.DB_CREATE)
Next, I defined the callback function, but I didn't understand how to
set the skey data & size attributes (as in the C example), so I tried
defining it as:
> def getname(secondary, pkey, pdata, skey):
> skey = pdata[0]
>
> dbp.associate(sdbp, getname)
i.e., I imagined the struct from the C example as a simple list in Python:
> dbp["WC42"] = ["Churchill","Winston"]
Unfortunately, the value associated with the "WC42" key needs to be a
string:
> File "c_example.py", line 22, in ?
> dbp["WC42"] = ["Churchill","Winston"]
> TypeError: Data values must be of type string or None.
So I tried this:
> dbp["WC42"] = "Churchill,Winston"
and redefined the getname() function as:
> def getname(secondary, pkey, pdata, skey):
> skey = re.split(pdata, ",")[0]
and while that version did persist the string "Churchill,Winston" to key
"WC42", I also got these warnings, which makes me think the secondary
index on last name was not created properly:
> TypeError: getname() takes exactly 4 arguments (2 given)
> TypeError: getname() takes exactly 4 arguments (2 given)
Does anyone have any suggestions, or are there Python/bsddb3 examples
posted anywhere?
More information about the pybsddb
mailing list