您好,欢迎访问一九零五行业门户网

JsonDB的一个使用实例

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
其它类似信息

推荐信息