JsonDB的一個使用實例

JsonDB是一個python實現的基於JSON格式的輕量級數據庫開源項目,你能夠經過github進行下載和安裝,項目地址是:https://github.com/hujiang001/jsonDB/python

做者提供了完整的參考文檔,並且是中文的,因此上手很是容易,這是他的中文文檔的地址: https://github.com/hujiang001/jsonDB/wiki/reference-document 。 當前在項目工程裏面也是包含了這個文檔:reference.md。git

項目還提供了一個使用的實例,在example.py中,下面咱們把這個例子粘貼出來,上面有詳細的註釋,能夠看出使用起來仍是挺簡單的:github

#!usr/bin/env python
# -*- coding: utf-8 -*-

from database import JSONDB

if __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
相關文章
相關標籤/搜索