Creating and populating a database
This tutorial shows you how to use the Python programming language to create an IBM® Cloudant® for IBM Cloud® database in your IBM Cloud service instance. You also learn how to populate the database with a simple collection of data.
This tutorial doesn't use the most efficient Python code. The intent is to show simple and easy-to-understand working code that you can learn from and apply to your own applications. You must apply normal best practices for checking and handling all warning or error conditions in your own applications.
Objectives
This tutorial provides a series of Python language instructions, suitable for the following tasks:
- Connecting to an IBM Cloudant service instance on IBM Cloud®.
- Creating a database within the service instance.
- Storing a small collection of data as documents within the database.
- Retrieving data.
- Deleting the database.
Before you begin
Prepare an IBM Cloudant service instance
-
Set up service credential requirements.
a. Create a service instance and credentials by following the Getting started tutorial.
b. Obtain your credentials by following the Locate your service credentials tutorial.
The tutorial uses the IAM credentials type for authentication.
Install Python and prepare the environment
-
Install the required version of Python.
You must have a current version of the Python programming language installed on your system.
a. Run the following command at a prompt to check that Python is installed:
python3 --version
b. Verify that you get a result similar to the following example:
Python 3.12.5
-
Create and activate a virtual environment
a. Create the virtual environment:
python3 -m venv cloudantdemo
This creates a sub-directory called
cloudantdemo
in the present working directory. You can choose another path.b. Activate the virtual environment:
source cloudantdemo/bin/activate
For more information about Python virtual environments and instructions for alternative operating systems see the Python standard library venv documentation.
-
Install the IBM Cloudant SDK for Python
pip install ibmcloudant
-
Verify the IBM Cloudant SDK for Python installation.
a. Check that the client library installed successfully by running the following command at a prompt:
pip show ibmcloudant
You get output with information about the
ibmcloudant
package.b. Inspect the output, which should start with lines similar to the following example:
Name: ibmcloudant Version: 0.9.1
-
Start the interactive Python interpreter
a. Run the
python
command in the virtual environment to launch the interpreterpython
b. Verify that you get output similar to the following example:
Python 3.12.5 Type "help", "copyright", "credits" or "license" for more information. >>>
Normally, you don't run commands individually in Python. You usually create a script, which is a list of the commands you want to run, stored in a Python file, with a
.py
extension.
Connecting to a service instance
-
Run these
import
statements to load the necessary SDK classes.from ibmcloudant.cloudant_v1 import CloudantV1 from ibm_cloud_sdk_core import ApiException from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
-
Find
host
andapikey
in your service credentials and replace the values inservice_host
andservice_api_key
in the following example.service_host = '{host}' service_api_key = '{apikey}'
Don't store your credentials in source code files. For real applications consider configuring your client from the environment.
-
Create a client configured with the service instance details.
client = CloudantV1(IAMAuthenticator(service_api_key)) client.set_service_url(f'https://{service_host}')
Here
https://
is prefixed to the host to make the service URL. Alternatively use theurl
from the service credentials, but if your instance is not IAM only be sure to remove the user information after the protocol and before the hostname. -
Get the server information with the
get_server_information
API to validate the connection.client.get_server_information().get_result()
Validate the output is similar to the following example:
{'couchdb': 'Welcome', 'version': '3.3.3+cloudant', 'vendor': {'name': 'IBM Cloudant', 'version': '8521', 'variant': 'paas'}, 'features': ['search', 'access-ready', 'iam', 'partitioned', 'pluggable-storage-engines', 'scheduler'], 'features_flags': ['partitioned']}
Now, your Python application can access the service instance on IBM Cloud.
Creating a database within the service instance
Next, you create a database within the service instance, called databasedemo
.
-
Define the database name with a variable in the Python application.
database_name = 'databasedemo'
-
Create the database using the
put_database
API.client.put_database(db=database_name).get_result()
Validate the database was created successfully with output:
{'ok': True}
Storing a small collection of data as documents within the database
You want to store a small, simple collection of data in the database. Use these data in other tutorials, like Using IBM Cloudant Query to find data.
-
Create sample data.
sample_data = [ [1, 'one', 'boiling', 100], [2, 'two', 'hot', 40], [3, 'three', 'hot', 75], [4, 'four', 'hot', 97], [5, 'five', 'warm', 20], [6, 'six', 'cold', 10], [7, 'seven', 'freezing', 0], [8, 'eight', 'freezing', -5] ]
-
Initialize a list of documents.
sample_docs = []
-
Iterate the sample data to make document data
For each row of the
sample_data
list create a dictionary mapping field names to the values from the row elements. Append each dictionary to thesample_docs
list.for row in sample_data: # Make a dictionary for each row document = { 'numberField': row[0], 'nameField': row[1], 'descriptionField': row[2], 'temperatureField': row[3] } # Append the dictionary to the list of documents sample_docs.append(document)
Python dictionaries are suitable for making JSON documents.
-
Create documents using the
post_document
API.for doc in sample_docs: client.post_document(db=database_name, document=doc).get_result()
This use of the
post_document
API automatically generates document IDs on the server. Alternatively include an ID in the document body or use theput_document
API to choose a specific document ID.For creating or modifying large numbers of documents in a single request there is a
post_bulk_docs
API.Validate that the output is similar to the following example.
{'ok': True, 'id': '43bb97b841c5b16c5ee44f4768e42efa', 'rev': '1-f998fc7b89d4466c1e7bb204b1b00f74'} {'ok': True, 'id': '480d1073dca0bf7bc9f28c2ad2f1383e', 'rev': '1-08b940a61ee2f4a013ba8f4abb307c70'} {'ok': True, 'id': '06266c9793afac3b5740872bc0f83d52', 'rev': '1-7de3d45186982b76243ce5879ccdbef4'} {'ok': True, 'id': '40867bf98071981da37d266d23b681ca', 'rev': '1-60206efd94ac6434740acd53c4278646'} {'ok': True, 'id': '622a70b1e0e9a0311284cd8bf5c439db', 'rev': '1-6d98db97adc12d2e4b114f96d2383a2d'} {'ok': True, 'id': '8c07fdbbf67d5173adc3b3034cd9202c', 'rev': '1-d97d8d0b6928bc743ccbe12b0621ad58'} {'ok': True, 'id': '4afb0c4e8d0c96d729dbf7081cbbe84c', 'rev': '1-462c5395df71106d903bedd29970ddeb'} {'ok': True, 'id': '33376a6ea0644abeef5462ff394755ef', 'rev': '1-523c33f6c5f82a2ae51a8df6366ee92b'}
Retrieving data
-
Retrieve a list of documents in the database using the
post_all_docs
API.all_docs_result = client.post_all_docs(db=database_name).get_result()
-
Iterate the returned rows to view document metadata.
for row in all_docs_result['rows']: print(row['id'])
Validate that the output is similar to the following example.
43bb97b841c5b16c5ee44f4768e42efa 480d1073dca0bf7bc9f28c2ad2f1383e 06266c9793afac3b5740872bc0f83d52 40867bf98071981da37d266d23b681ca 622a70b1e0e9a0311284cd8bf5c439db 8c07fdbbf67d5173adc3b3034cd9202c 4afb0c4e8d0c96d729dbf7081cbbe84c 33376a6ea0644abeef5462ff394755ef
These IDs will match those from the previous creation step.
In a relational database, the first document stored in a database is always the first document returned in a list of results. This notion doesn't necessarily apply to document-oriented databases, such as IBM Cloudant.
Full retrieval of a document
-
Get the ID of the first document in the database.
first_doc_id = all_docs_result['rows'][0]['id']
This uses the ID from listing the documents previously. You can also store the ID from the response after creating the document.
-
Retrieve the document content using the
get_document
API.client.get_document(db=database_name, doc_id=first_doc_id).get_result()
Validate that the output is similar to the following example.
{'_id': '43bb97b841c5b16c5ee44f4768e42efa', '_rev': '1-f998fc7b89d4466c1e7bb204b1b00f74', 'numberField': 1, 'nameField': 'one', 'descriptionField': 'boiling', 'temperatureField': 100}
Deleting the database
-
Delete the database using the
delete_database
API.try: client.delete_database(db=database_name).get_result() except ApiException as ae: print(f'There was a problem deleting database {database_name}. HTTP status code {ae.status_code}. Error message {ae.message}.')
Validate the database was deleted successfully with output:
{'ok': True}
-
Review the basic error handling demonstrating one way to handle problems.
Finishing up
-
End the interactive Python interpreter session by issuing an
EOF
for example withCtrl+D
. -
Optionally deactivate and delete the Python virtual environment.
a. Deactivate the virtual environment
deactivate
b. Delete the virtual environment.
rm -r cloudantdemo