---
title: "Python 操作BDB (1)"
author: "hammer_nail"
date: "2009-09-13T22:45:21Z"
tags: ["C", "C#", "C++", "Python"]
slug: "python-bdb-1"
draft: false
lastmod: "2009-09-13T22:45:21Z"
id: 468022
url: "https://hammer-nail.iteye.com/blog/468022"
---

Python 对BDB的访问支持的非常好，在内建的lib里就有对BDB的访问支持,

见博文 http://gashero.yeax.com/?p=5#id14

 

我简单写了个K-v的例子，记录如下：

 

```
'''
Created on 2009-9-13

@author: Administrator
'''
import bsddb  
class BDBTest(object):

    def __init__(self):

        self.db = bsddb.btopen('test.db', 'c')
        print "Opened"
        self.db["2009-08-14 22:00"] = "gg"
        self.db["2009-08-15 22:00"] = "cc"
        self.db["2009-07-15 00:00"] = "tt"
        self.db["2009-08-16 22:00"] = "gg"
        self.db["2009-08-16 23:00"] = "gx"
        # K-V
        print self.db["2009-08-14 22:00"]
        # Use Previous and next allocate the cursor
        self.db.set_location('2009-08-15')
        # 
        print self.db.next()
        
        print self.db.previous() 
        
        #d = bsddb.hashopen("aaa.db", "c")
        
if __name__ == '__main__':
    try:
        bdb = BDBTest()
    except:
        print "Exception Happen"
```

 

在接下来的系列里，还会对BDB的的访问方式进行进一步的探究。