Select Page

KidsData

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.

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.

Create

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

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

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.

Read

Knowing the id, reading a document is straightforward.

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().

Update

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.

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.

Delete

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

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.

Share This