JsonDB是一個python實現的基於JSON格式的輕量級資料庫開源項目,你可以透過github進行下載和安裝,項目位址是:https://github.com/hujiang001/jsonDB/ 。
作者提供了完整的參考文檔,而且是中文的,所以上手非常容易,這是他的中文文檔的地址:https://github.com/hujiang001/jsonDB/wiki/reference-document 。 目前在專案工程裡面也是包含了這個文件:reference.md。
專案也提供了一個使用的實例,在example.py中,下面我們把這個例子貼出來,上面有詳細的註釋,可以看出使用起來還是挺簡單的:
#!usr/bin/env python# -*- coding: utf-8 -*-from database import JSONDBif __name__=="__main__": shopDb = JSONDB('shop_db',hashSize=1) #create shop db userDb = JSONDB('sellerDb',hashSize=1) #create user db #specify key shopDb.ensureKey('shop',['id']) shopDb.ensureKey('goods',['id']) userDb.ensureKey('seller',['id','name']) userDb.ensureKey('customer',['id','name']) #for debugging, you can start perfDot and open debug switch shopDb.perfDotStart() userDb.perfDotStart() shopDb.debugSwitch(1) userDb.debugSwitch(1) #insert data #here we insert some data for i in range(0,100): #insert data one by one shopDb.insert('shop',[{'id':i, 'name':'my shop', 'description':'this is a telephone shop'}]) #inset a data list goodsList = [{'id':10001, 'class':'telephone', 'brand':'apple', 'color':'white'}, {'id':10002, 'class':'telephone', 'brand':'huawei', 'color':'black'}, {'id':10003, 'class':'telephone', 'brand':'zte', 'color':'white'}, {'id':10004, 'class':'telephone', 'brand':'xiaomi', 'color':'black'}, {'id':10005, 'class':'telephone', 'brand':'moto', 'color':'white'}, {'id':10006, 'class':'telephone', 'brand':'oppo', 'color':'black'},] shopDb.insert('goods',goodsList) sellersList = [{'id':0x34200,'name':'li','sex':'female','birth':'1985-09-03','tel':'0816-19876545432'}, {'id':0x34201,'name':'wang','sex':'male','birth':'1988-11-03','tel':'0816-2345453453'}, {'id':0x34202,'name':'hong','sex':'female','birth':'1995-10-19','tel':'0816-144567589'}] userDb.insert('seller',sellersList) customersList = [{'id':0x7800,'name':'zhu','sex':'female','profile':['dress','mother','sport']}, {'id':0x7801,'name':'rong','sex':'male','profile':['baby','read','tech']}, {'id':0x7802,'name':'guo','sex':'female','profile':['phone','dress','food']}] userDb.insert('customer',customersList) #export to default file shopDb.exportToFile() userDb.exportToFile(fileName='user') #find # find with key findList = shopDb.find('goods',filter={'id':{'$lt':10004}}) JSONDB.rprint(findList) findList = shopDb.find('goods',filter={'id':10004}) JSONDB.rprint(findList) findList = userDb.find('customer',filter={'name':'guo'}) JSONDB.rprint(findList) #delete shopDb.delete('goods',{'id':10005}) #update shopDb.update('goods',set={'class':'phone'}) #merge shopDb.merge(userDb) shopDb.exportToFile('mergeDb') #show pref time shopDb.perfDotEnd() userDb.perfDotEnd() #import importDb = JSONDB('importDb') importDb.debugSwitch(1) importDb.importFromFile(fileName='mergeDb') #importDb.printAll() importDb.exportToFile() print importDb