Over a million developers have joined DZone.

Exploring Couchbase Mobile on Android: C.R.U.D.

In this article, we take a look at how to use Couchbase Mobile on your Android and show how to implement some CRUD operations.

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Kids exploring

Photo by Ken Banks used with permission under CC BY 2.0

In previous posts, I wrote a mid-level overview of Couchbase Mobile and walked through getting set up in an Android project.

In this post, I want to begin exploring how to use Couchbase Lite (CBL) in an Android app.

We'll start with showing how to Create, Read, Update, and Delete some data. (This is where the CRUD acronym comes from that you'll often see in the database world.)

We'll progress into more sophisticated uses in later posts, but really, you can do a lot of useful things just with these steps.

Almost everything occurs as an operation on a document object. Using CBL turns out to be easier than most any other approach, even for extremely simple purposes.

Opening or creating a database

To perform any database operation, first, we need an instance of the Couchbase Lite Manager class. You'll see in the code below, you don't really use this class for much in simple cases.

Using a manager instance, opening a database is just one line. Let's take a look.

Manager manager = null;
Database database = null;

try {
    manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
    database = manager.getDatabase("crud");
} catch (Exception ex) {
    Log.e(TAG, "Error getting database");

Notice the first argument to the Manager class constructor. This creates an AndroidContext, which is something unique to Couchbase. Be sure to pass in an application context to initialize this object, as shown. Anything else risks leaking memory.

The database name is something I made up for this example. You can use pretty much anything you want.


Creating a new database entry (referred to as a document) is just one line.

Document document = database.createDocument();

Often you'll want to know the unique id of the document. Here's the code to retrieve it.

String documentId = document.getId();

The createDocument method creates a document with a system-defined unique id. For simple applications, you may want to specify your own id. In this case, use getDocument(String id) instead. If the document doesn't already exist, it will be created.


Knowing the id, reading a document is straightforward.

document = database.getDocument(documentId);

You can do more sophisticated work with documents and retrieval. We'll get into that another time when we delve into queries.

With a document in hand, you can pull data out of it directly using getProperty(String key) or getProperties().


Couchbase Lite documents are stored as JSON. That makes it easy to manipulate them. Once again, you don't really need to do anything to get going. You can manipulate documents directly using maps. Here's what the code looks like to update some profile information, in this approach.

Map<String, Object> profile = new HashMap<>();
profile.put("firstName", "Hod");
profile.put("lastName", "Greeley");

try {
} catch (CouchbaseLiteException ex) {
    Log.e(TAG, "CBL operation failed");

One of the most compelling reasons to use CBL is the flexibility it gives you. This code shows how you can add a new item to your document.

profile = new HashMap<>();
profile.put("type", "profile");  // Add a "type" to the document

try {
} catch (CouchbaseLiteException ex) {
    Log.e(TAG, "CBL operation failed");


Call the delete method on a document to remove it from the database. The delete happens immediately

try {
} catch (CouchbaseLiteException ex) {
    Log.e(TAG, "CBL failed deleting document");

Wrapping up

What really strikes me in this post is how much more object oriented using CBL feels, compared to something like SQLite. I hope that comes through. Explaining that you call the delete method on a document to delete it feels almost silly. But, of course, that's not how it works in a lot of databases.


You can follow me personally on Twitter @HodGreeley

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.

app,crud,android app,couchbase,android,database,mobile

Published at DZone with permission of Hod Greeley. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}