欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用Berkeley DB數(shù)據(jù)庫實例

 更新時間:2014年09月26日 09:11:17   投稿:shichen2014  
這篇文章主要介紹了python使用Berkeley DB數(shù)據(jù)庫的方法,以實例形式講述了完整的操作過程,并總結(jié)了具體的操作步驟,非常具有實用性,需要的朋友可以參考下

本文實例講述了python使用Berkeley DB數(shù)據(jù)庫的方法,分享給大家供大家參考。

具體實現(xiàn)方法如下:

try: 
  from bsddb import db 
except ImportError: 
  from bsddb3 import db 
print db.DB_VERSION_STRING 
#檢測是否有bsddb包 
 
def irecords(curs): 
  record = curs.first() 
  while record: 
    yield record 
    record = curs.next() 
     
adb = db.DB() 
adb.open('db_filename',dbtype = db.DB_HASH, flags = db.DB_CREATE) 
for i,w in enumerate('some word for example'.split()): 
  adb.put(w,str(i)) 
   
for key, data in irecords(adb.cursor()): 
  print key,data 
adb.close() 
print '*'*60 
# 
the_same_db = db.DB() 
the_same_db.open("db_filename") 
the_same_db.put('skidoo','23')#加入數(shù)據(jù)庫 
the_same_db.put('for','change the data')#改變數(shù)據(jù)庫的數(shù)據(jù) 
for key, data in irecords(the_same_db.cursor()): 
  print key,data 
the_same_db.close()

運行結(jié)果如下:  

Berkeley DB 4.7.25: (May 15, 2008)
example 3
some 0
word 1
for 2
************************************************************
example 3
some 0
word 1
for change the data
skidoo 23

這里再總結(jié)一下操作步驟:

1.先初始化數(shù)據(jù)庫

adb = db.DB()

2.打開數(shù)據(jù)庫

adb.open('db_filename',dbtype = db.DB_HASH, flags = db.DB_CREATE)

3.插入或修改數(shù)據(jù)庫中的數(shù)據(jù)

adb.put('skidoo','23')#加入數(shù)據(jù)庫
adb.put('for','change the data')#改變數(shù)據(jù)庫的數(shù)據(jù)

4.關(guān)閉數(shù)據(jù)庫

adb.close()

希望本文所述對大家的Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論