Skip to content

Instantly share code, notes, and snippets.

@doneill
Created February 9, 2015 00:54
Show Gist options
  • Save doneill/d1abcf6fe4b369078589 to your computer and use it in GitHub Desktop.
Save doneill/d1abcf6fe4b369078589 to your computer and use it in GitHub Desktop.
Basic Couchbase Lite CRUD operations
final String TAG = "Hello-CouchBase";
Log.d(TAG, "Begin app");
// create a manager
Manager manager;
try{
manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
Log.d(TAG, "Manager created");
}catch (IOException e){
Log.e(TAG, "Cannot create Manager object");
return;
}
// create a name for the database and make sure the name is legal
String dbname = "hello";
if(!Manager.isValidDatabaseName(dbname)){
Log.e(TAG, "Bad database name");
return;
}
// create a new database
Database database;
try {
database = manager.getDatabase(dbname);
Log.d(TAG, "Database created");
}catch (CouchbaseLiteException e){
Log.e(TAG, "Cannot get database");
return;
}
// get the current date and time
SimpleDateFormat dataFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Calendar calendar = GregorianCalendar.getInstance();
String currentTimeString = dataFormatter.format(calendar.getTime());
// create an object that contains data for a document
Map<String, Object> docContent = new HashMap<String, Object>();
docContent.put("message", "Hello Couchbase Lite");
docContent.put("creationDate", currentTimeString);
// display the data for the new document
Log.d(TAG, "docContent =" + String.valueOf(docContent));
// create an empty document
Document document = database.createDocument();
// add content to document and write the document to the database
try {
document.putProperties(docContent);
Log.d(TAG, "Document written to database named " + dbname + " with ID = " + document.getId());
} catch (CouchbaseLiteException e){
Log.e(TAG, "Cannot write document to database", e);
}
// save the ID of the new document
String docID = document.getId();
// retrieve the document from the database
Document retrievedDocument = database.getDocument(docID);
// display the retrieved document
Log.d(TAG, "retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
// update the document
Map<String, Object> updatedProperties = new HashMap<String, Object>();
updatedProperties.putAll(retrievedDocument.getProperties());
updatedProperties.put("message", "We're having a heat wave!");
updatedProperties.put("temperature", "95");
try {
retrievedDocument.putProperties(updatedProperties);
Log.d(TAG, "updated retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
}catch (CouchbaseLiteException e){
Log.e(TAG, "Cannot update document", e);
}
// delete the document
try {
retrievedDocument.delete();
Log.d(TAG, "Deleted document, deletion status = " + retrievedDocument.isDeleted());
}catch (CouchbaseLiteException e){
Log.e(TAG, "Cannot delete document", e);
}
Log.d(TAG, "End app");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment