Aug 16, 2017

Code diet with Sugar ORM

blogpost author photo
Kamila Karasińska
blogpost cover image
Data storage is a very important topic not only for backend developers. From the mobile developer point of view, most of the work is communication with REST API, but we sometimes need to implement a classical database in our application. 

We are facing a choice: simple, typical SQLite implementation or ORM one? There are many differences between them, especially in the amount of code that you need to write in order to have a functional model. In traditional database coding we must first extend SQLiteOpenHelper, fill that class with necessary data (database and tables names, version), override onCreate() and onUpgrade(). Next, it’s time to create Manager class and write methods which allow for operation on our data. It’s a lot of work, especially if you need a complex, big database with many relations. Let’s try to simplify it!

First Preparations

Sugar ORM is an easy way to work with data and has really minimal requirements to be ready to work! Installation is straightforward - you just need to add a right line to your grade file: 

compile 'com.github.satyan:sugar:1.3'

and configure SugarApp as an application class and change android:name in your AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""


        <meta-data android:name="DATABASE" android:value="database.db" />
        <meta-data android:name="VERSION" android:value="2" />
        <meta-data android:name="QUERY_LOG" android:value="true" />
        <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.binarapps" />



As you can see, you also need to add a few meta-data attributes: database is a name of your database, version - it’s version, domain package name - name of your package, you can turn on query logs too. After this preparations, we can write models corresponding entities in our database.

Creating Entities

Now, let’s define first model Task and create its fields, but remember: it should have a public, default constructor! Every field will be the field in the table.

public class Task extends SugarRecord {
    String title;
    String description;
    long time;

    public Task() {

    public Task(String title, String description, long time){
        this.title = title;
        this.description = description;
        this.time = time;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public long getTime() {
        return time;

    public void setTime(long time) {
        this.time = time;



It’s all that you can need. Let the magic happen - sugar will generate tables for you. Making queries and basic CRUD methods - it was never as easy before. We can save our task:

Task task = new Task("title", "description", System.currentTimeMillis());;

Update task (of course if you have an object id - Sugar expects long value):

Task task = Task.findById(Task.class, id);
task.setTitle("updated title here");


Task task = Task.findById(Task.class, id);

Bulk methods

Sugar generally eliminates the need to perform queries. Instead, we have static methods for data management - for example: 

List<Task> tasks = Task.listAll(Task.class);

Listing with conditions, if you like SQL queries ;)

Task.find(Task.class, "title = ?", "task_name");

Current, stable version 1.3 gives you the opportunity to use the count method:

long counter = Task.count(Task.class, null, null);

Query builder

Sugar ORM allows us to use a nice query builder. Every query method returns back object and you can chain filters and queries easily. We create a query that gives all tasks created before today:

        Select query = Select.from(Task.class)


First, we need to create a folder named "sugar_upgrades" in "assets" of your project and create "<version>.sql" which version is VERSION value declared in your manifest file. Next, change present VERSION value to upgraded. If the current version is 1 and upgraded is 3, Sugar looks for 2.sql and creates new tables and fields.

Experienced developers may be accustomed to classical implementations of databases in Android applications, but the traditional way is sometimes frustrating and requires long debugging time. ORM libraries like Sugar make creating a database much cleaner and less bug-prone experience. If you care about your time I recommend to use those libraries. You will save a lot of time and focus on what matters most - the business logic of your applications.