Python CouchDB and pythoncouchdb

Source: Internet
Author: User
Tags couchdb

Python CouchDB and pythoncouchdb

Install the python couchDb Library:

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

Connect to the server

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

Create a database

>>> Db = couch. create ('test') # create a database> db = couch ['mydb'] # use an existing database

Create a document and insert it to the database:

>>> Doc = {'foo': 'bar'} >>> db. save (doc) ('taobao', '1-taobao') >>> doc {'_ rev': '1-4c6114c65e295552ab1019e2b046b10e ', 'foo': 'bar ', '_ id': 'e0658cab843b59e63c8779a9a5000b01'} the save () method returns the '_ id',' _ rev' Field

Query a database by id

>>> db['e0658cab843b59e63c8779a9a5000b01']<Document 'e0658cab843b59e63c8779a9a5000b01'@'1-4c6114c65e295552ab1019e2b046b10e' {'foo': 'bar'}>

Update document:

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

Traverse Database

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

Delete documents and clear Databases

>>> db.delete(doc)>>> couch.delete('test')

Repeated errors occur when Python calls CouchDB insert.

You can use pydoc couchdb to query its documentation
 
How does python operate windows Services?

You can use the OS module to operate services in windows.

The command to query a service status is:
SC query [servicename]

For example, to query the WebClient Service Status:

C: \ Documents ents and Settings \ Administrator> SC query WebClient

SERVICE_NAME: WebClient
TYPE: 10 WIN32_OWN_PROCESS
STATE: 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE: 0 (0x0)
SERVICE_EXIT_CODE: 0 (0x0)
CHECKPOINT: 0x0
WAIT_HINT: 0x0

Therefore, the starting point of this python script can first query the status of a service. If the status is not RUNNING, start it. The key code is as follows:

Result = OS. popen ("SC query WebClient ")
If not "RUNNING" in result:
OS. system ("SC start WebClient ")

Let the script run on a regular basis, and hope to help you

Related Article

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.