Using Python to Operate IBM Cloudant
Using Python to operate IBM Cloudant databases requires the installation of the 'Cloudant' library.
The following is an example code for connection, data insertion, query, modification, and deletion:
python
from cloudant.client import Cloudant
#Connect to Cloudant database
client = Cloudant("USERNAME", "PASSWORD", url="URL")
client.connect()
#Select Database
db = client['DATABASE_NAME']
#Insert Data
data = {
"name": "John",
"age": 25,
"email": "john@example.com"
}
doc = db.create_document(data)
if doc.exists():
Print ("Data insertion successful")
#Query data
selector = {
"name": "John"
}
docs = db.get_query_result(selector)
for doc in docs:
print(doc)
#Modify data
doc = db['DOCUMENT_ID']
doc['age'] = 30
doc.save()
Print ("Data modified successfully")
#Delete data
doc = db['DOCUMENT_ID']
doc.delete()
Print ("Data deleted successfully")
#Disconnect
client.disconnect()
Please include 'USERNAME', 'PASSWORD', 'URL', and 'DATABASE'_ Replace 'NAME' with the actual value. In addition, 'Document'_ ID 'is the ID of the document object, which can be obtained when inserting data.