Python CouchDB database simple example, pythoncouchdb
Install the python couchDb Library:
Https://pypi.python.org/pypi/CouchDB/0.10
Connect to the server
Copy codeThe Code is as follows:
>>> Import couchdb
>>> Couch = couchdb. Server ('HTTP: // example.com: 5984 /')
Create a database
Copy codeThe Code is as follows:
>>> Db = couch. create ('test') # create a database
>>> Db = couch ['mydb'] # use an existing database
Create a document and insert it to the database:
Copy codeThe Code is 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' fields.
Query a database by id
Copy codeThe Code is as follows:
>>> Db ['e0658cab843b59e63c8779a9a5000b01 ']
<Document 'e0658cab843b59e63c8779a9a5000b01 '@ '1-4c6114c65e295552ab1019e2b046b10e' {'foo': 'bar'}>
Update document:
Copy codeThe Code is as follows:
>>> Data = db ["5fecc0d7fe5ac6b46359b5eec4f3ff"]
>>> Data ['billseconds '] = 191
>>> Db. save (data)
(U '5fecc0d7fe5ac6b46359b5eec4f3ff ', u '3-6b8a6bb9f2428c510dcacdd5c918d632 ')
Traverse Database
Copy codeThe Code is as follows:
>>> For id in db:
... Print id
...
'E0658cab843b59e63c8779a9a5000b01'
Delete documents and clear Databases
Copy codeThe Code is as follows:
>>> Db. delete (doc)
>>> Couch. delete ('test ')