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