To install the Python couchdb library:
https://pypi.python.org/pypi/CouchDB/0.10
Connecting to a server
Copy Code code as follows:
>>> Import Couchdb
>>> couch = couchdb. Server (' http://example.com:5984/')
Creating a Database
Copy Code code as follows:
>>> db = couch.create (' Test ') # New database
>>> db = couch[' mydb '] # using a database that already exists
to create a document and insert it into a database:
Copy Code code as follows:
>>> doc = {' foo ': ' Bar '}
>>> Db.save (DOC)
(' E0658cab843b59e63c8779a9a5000b01 ', ' 1-4c6114c65e295552ab1019e2b046b10e ')
>>> doc
{' _rev ': ' 1-4c6114c65e295552ab1019e2b046b10e ', ' foo ': ' Bar ', ' _id ': ' E0658cab843b59e63c8779a9a5000b01 '}
The Save () method returns the ' _id ', ' _rev ' field
querying the database by ID
Copy Code code as follows:
>>> db[' E0658CAB843B59E63C8779A9A5000B01 ']
<document ' e0658cab843b59e63c8779a9a5000b01 ' @ ' 1-4c6114c65e295552ab1019e2b046b10e ' {' foo ': ' Bar '}>
To update a document:
Copy Code code as follows:
>>> data = db["5FECC0D7FE5ACAC6B46359B5EEC4F3FF"]
>>> data[' billseconds ' = 191
>>> Db.save (data)
(U ' 5fecc0d7fe5acac6b46359b5eec4f3ff ', U ' 3-6b8a6bb9f2428c510dcacdd5c918d632 ')
traversing the database
Copy Code code as follows:
>>> for ID in DB:
... print ID
...
' E0658cab843b59e63c8779a9a5000b01 '
Delete a document and clean up the database
Copy Code code as follows:
>>> Db.delete (DOC)
>>> couch.delete (' test ')