Python操作CouchDB資料庫簡單樣本

來源:互聯網
上載者:User
安裝python couchDb庫:

https://pypi.python.org/pypi/CouchDB/0.10

串連伺服器
複製代碼 代碼如下:


>>> import couchdb
>>> couch = couchdb.Server('http://example.com:5984/')


建立資料庫
複製代碼 代碼如下:


>>> db = couch.create('test') # 建立資料庫
>>> db = couch['mydb'] # 使用已經存在的資料庫


建立文檔並插入到資料庫:
複製代碼 代碼如下:


>>> doc = {'foo': 'bar'}
>>> db.save(doc)
('e0658cab843b59e63c8779a9a5000b01', '1-4c6114c65e295552ab1019e2b046b10e')
>>> doc
{'_rev': '1-4c6114c65e295552ab1019e2b046b10e', 'foo': 'bar', '_id': 'e0658cab843b59e63c8779a9a5000b01'}


save()方法會返回'_id','_rev'欄位
通過id查詢資料庫
複製代碼 代碼如下:


>>> db['e0658cab843b59e63c8779a9a5000b01']


更新文檔 :
複製代碼 代碼如下:


>>> data = db["5fecc0d7fe5acac6b46359b5eec4f3ff"]
>>> data['billSeconds'] = 191
>>> db.save(data)
(u'5fecc0d7fe5acac6b46359b5eec4f3ff', u'3-6b8a6bb9f2428c510dcacdd5c918d632')


遍曆資料庫
複製代碼 代碼如下:


>>> for id in db:
... print id
...
'e0658cab843b59e63c8779a9a5000b01'


刪除文檔並清理資料庫
複製代碼 代碼如下:


>>> db.delete(doc)
>>> couch.delete('test')
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.