安裝python couchDb庫(kù):
https://pypi.python.org/pypi/CouchDB/0.10
連接服務(wù)器
>>> import couchdb
>>> couch = couchdb.Server('http://example.com:5984/')
創(chuàng)建數(shù)據(jù)庫(kù)
>>> db = couch.create('test') # 新建數(shù)據(jù)庫(kù)
>>> db = couch['mydb'] # 使用已經(jīng)存在的數(shù)據(jù)庫(kù)
創(chuàng)建文檔并插入到數(shù)據(jù)庫(kù):
>>> doc = {'foo': 'bar'}
>>> db.save(doc)
('e0658cab843b59e63c8779a9a5000b01', '1-4c6114c65e295552ab1019e2b046b10e')
>>> doc
{'_rev': '1-4c6114c65e295552ab1019e2b046b10e', 'foo': 'bar', '_id': 'e0658cab843b59e63c8779a9a5000b01'}
save()方法會(huì)返回'_id','_rev'字段
通過id查詢數(shù)據(jù)庫(kù)
>>> db['e0658cab843b59e63c8779a9a5000b01']
<Document 'e0658cab843b59e63c8779a9a5000b01'@'1-4c6114c65e295552ab1019e2b046b10e' {'foo': 'bar'}>
更新文檔 :
>>> data = db["5fecc0d7fe5acac6b46359b5eec4f3ff"]
>>> data['billSeconds'] = 191
>>> db.save(data)
(u'5fecc0d7fe5acac6b46359b5eec4f3ff', u'3-6b8a6bb9f2428c510dcacdd5c918d632')
遍歷數(shù)據(jù)庫(kù)
>>> for id in db:
... print id
...
'e0658cab843b59e63c8779a9a5000b01'
刪除文檔并清理數(shù)據(jù)庫(kù)
>>> db.delete(doc)
>>> couch.delete('test')