Ways to manipulate couchdb in Python

Source: Internet
Author: User
This article briefly describes the Python operation Couchdb method, share to everyone for your reference. Here's how:

1. Install the Python couchdb library:

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

2. Connect to the server

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

3. Create a Database

>>> db = couch.create (' Test ') # New database >>> db = couch[' mydb ') # using a database that already exists

4. Create the document and insert it into the database:

>>> doc = {' foo ': ' Bar '}>>> Db.save (Doc) (' E0658cab843b59e63c8779a9a5000b01 ', ' 1-4c6114c65e295552ab1019e2b046b10e ') >>> doc{' _rev ': ' 1-4c6114c65e295552ab1019e2b046b10e ', ' foo ': ' Bar ', ' _ Id ': ' e0658cab843b59e63c8779a9a5000b01 '}

The Save () method returns ' _id ', ' _rev ' field

5. Querying the database by ID

>>> db[' E0658CAB843B59E63C8779A9A5000B01 ']
 
  

6. Update the Documentation:

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

7. Traversing the database

>>> for ID in db: ...   Print ID ... ' e0658cab843b59e63c8779a9a5000b01 '

8. Delete the document and clean up the database

>>> Db.delete (DOC) >>> couch.delete (' test ')

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.