This article describes how to operate CouchDB in python, including common operations such as couchDb Library installation, server connection, database creation, database query, and database traversal, for more information about how to operate CouchDB in python, see this article. The specific method is as follows:
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') # create a database> db = couch ['mydb'] # Use an existing database
4. create a document and insert it to the database:
>>> doc = {'foo': 'bar'}>>> db.save(doc)('e0658cab843b59e63c8779a9a5000b01', '1-4c6114c65e295552ab1019e2b046b10e')>>> doc{'_rev': '1-4c6114c65e295552ab1019e2b046b10e', 'foo': 'bar', '_id': 'e0658cab843b59e63c8779a9a5000b01'}
The save () method returns the '_ id',' _ Rev' fields.
5. query the database by id
>>> db['e0658cab843b59e63c8779a9a5000b01']
6. update the document:
>>> data = db["5fecc0d7fe5acac6b46359b5eec4f3ff"] >>> data['billSeconds'] = 191>>> db.save(data)(u'5fecc0d7fe5acac6b46359b5eec4f3ff', u'3-6b8a6bb9f2428c510dcacdd5c918d632')
7. traverse the database
>>> for id in db:... print id...'e0658cab843b59e63c8779a9a5000b01'
8. delete documents and clear databases
>>> db.delete(doc)>>> couch.delete('test')
I hope this article will help you with Python programming.