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"); } 1 2 3 4 5 6 7 8 9 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.

Create

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

Document document = database.createDocument(); 1 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(); 1 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.

Read

Knowing the id, reading a document is straightforward.

document = database.getDocument(documentId); 1 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() .

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.

Map<String, Object> profile = new HashMap<>(); profile.put("firstName", "Hod"); profile.put("lastName", "Greeley"); try { document.putProperties(profile); } catch (CouchbaseLiteException ex) { Log.e(TAG, "CBL operation failed"); } 1 2 3 4 5 6 7 8 9 Map < String , Object > profile = new HashMap < > ( ) ; profile . put ( "firstName" , "Hod" ) ; profile . put ( "lastName" , "Greeley" ) ; try { document . putProperties ( profile ) ; } 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.putAll(document.getProperties()); profile.put("type", "profile"); // Add a "type" to the document try { document.putProperties(profile); } catch (CouchbaseLiteException ex) { Log.e(TAG, "CBL operation failed"); } 1 2 3 4 5 6 7 8 9 profile = new HashMap < > ( ) ; profile . putAll ( document . getProperties ( ) ) ; profile . put ( "type" , "profile" ) ; // Add a "type" to the document try { document . putProperties ( profile ) ; } catch ( CouchbaseLiteException ex ) { Log . e ( TAG , "CBL operation failed" ) ; }

Delete

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

try { document.delete(); } catch (CouchbaseLiteException ex) { Log.e(TAG, "CBL failed deleting document"); } 1 2 3 4 5 try { document . delete ( ) ; } 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.

Postscript