Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

This tutorial describes how to work with the Room framework to manage SQLite database in Android applications.

1. SQLite and Android

SQLite is an Open Source database. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements. The database requires limited memory at runtime (approx. 250 KByte).

SQLite supports the following data types:

  • TEXT(similar to String in Java)

  • INTEGER(similar to long in Java)

  • REAL (similar to double in Java). All other types must be converted into one of these fields before getting saved in the database. SQLite itself does not validate if the types written to the columns are actually of the defined type. This means you can write an integer into a string column and bvice versa.

Access to an SQLite database involves accessing the file system. This can be slow. Therefore it is recommended to perform database operations asynchronously.

More information about SQLite can be found on the SQLite website: http://www.sqlite.org.

2. Using Room as SQL object mapping library

Room is an annotation processing based SQL object mapping library provided by Google. Room is designed to abstract away the underlying database tables and queries. Therefore it provides an easy way to create and use Sqlite database. It is based on best-practices for persisting data in databases. For example, Room does, by default, not allow database access in the main thread.

Room has 3 major components:

  • Database: define an abstract database class which provides one or more data access objects (dao).

  • Dao: interface which defines how to get or change the values in db.

  • Entity: represents a value object in the db.

For every table in your database you define a Java class, annotated with @Entity. The primary key must be annotated with @PrimaryKey.

For each Java annotated with @Entity, you define an interface for the database access object (DAO) annotated with @Dao. In this interface you define methods annotated with:

  • @Query

  • @Insert

  • @Delete

@Query are asynchronous, while @Insert and @Delete are synchronous.

The generator creates readable error messages, if the database statements is erroneous.

To connect your data to your UI, you can use LiveData. LiveData is an observable data holder. It notifies observers when data changes so that the user interface can be updated.

You can also define Type Converters to define how custom types can be converted into types known by the database. @Ignore can be used to ignore certain fields.

Room does not support object references between entities, to avoid potential performances issues. Even though you cannot use direct relationships, it is possible to define foreign key constraints between entities.

3. Exercise using Room

This exercise demonstrates the usage of root to persist data. It also demonstrates the usage of foreign constraints to model relationships.

3.1. Create project

Create a project called com.vogella.android.persistence with the same top-level package name. Use the Empty Activity template.

Ensure google() is available in the main build.gradle file.

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

Add the following dependencies to your app/build.gradle file.

    compile 'android.arch.lifecycle:extensions:1.0.0-rc1';
    compile 'android.arch.persistence.room:runtime:1.0.0-rc1';
    annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-rc1';
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-rc1';
}

3.2. Create database classes and use them

Create the data objects.

package com.vogella.android.persistence;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

/**
 * Created by vogella on 06.09.17.
 */
@Entity
public class User {

    @PrimaryKey
    public final int id;
    public String name;
    public int level;
    public long skillPoints;


    public User(int id, String name, long skillPoints) {
        this.id = id;
        this.name = name;
        this.skillPoints  = skillPoints;
        this.level = 0;
    }

}
package com.vogella.android.persistence;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.ForeignKey;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;

@Entity(tableName = "trophy",
        foreignKeys = {
                @ForeignKey(
                        entity = User.class,
                        parentColumns = "id",
                        childColumns = "userId",
                        onDelete = ForeignKey.CASCADE
                )},
        indices = { @Index(value = "id")}
)
public class Trophy {

    @PrimaryKey(autoGenerate = true)
    long id;

    public long userId;

    String description;

    public Trophy(long userId, String description) {
        this.userId = userId;
        this.description = description;
    }
}

Create the following DAO object.

package com.vogella.android.persistence;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import java.util.List;


@Dao
public interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void addUser(User user);

    @Query("select * from user")
    public List<User> getAllUser();

    @Query("select * from user where id = :userId")
    public List<User> getUser(long userId);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateUser(User user);

    @Query("delete from user")
    void removeAllUsers();
}
package com.vogella.android.persistence;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import java.util.List;


@Dao
public interface TrophyDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void addTrophy(Trophy trophy);

    @Query("SELECT * FROM trophy WHERE userId=:userId")
    List<Trophy> findTrophiesForUser(int userId);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateTrophy(Trophy trophy);

    @Query("delete from trophy where id = :id")
    void delete(long id);

}

Create the AppDatabase class.

package com.vogella.android.persistence;


import android.content.Context;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;

@Database(entities = {User.class,  Trophy.class
}, version = 16, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract UserDao userDao();
    public abstract TrophyDao trophyDao();

    public static AppDatabase getDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context, AppDatabase.class, "userdatabase")
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class)
                            // To simplify the exercise, allow queries on the main thread.
                            // Don't do this on a real app!
                            .allowMainThreadQueries()
                            // recreate the database if necessary
                            .fallbackToDestructiveMigration()
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

Adjust your main activity to use the database.

package com.vogella.android.persistence;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends Activity {

    private User user;
    private AppDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        database = AppDatabase.getDatabase(getApplicationContext());

        // cleanup for testing some initial data
        database.userDao().removeAllUsers();
        // add some data
        List<User> users = database.userDao().getAllUser();
        if (users.size()==0) {
            database.userDao().addUser(new User(1, "Test 1", 1));
            user = database.userDao().getAllUser().get(0);
            Toast.makeText(this, String.valueOf(user.id), Toast.LENGTH_SHORT).show();
            Trophy trophy = new Trophy(user.id, "Learned to use 3");
            database.trophyDao().addTrophy(trophy);
            database.userDao().addUser(new User(2, "Test 2", 2));
            database.userDao().addUser(new User(3, "Test 3", 3));
        }

        updateFirstUserData();

    }


    private void updateFirstUserData() {
        List<User> user = database.userDao().getAllUser();
        List<Trophy> trophiesForUser = database.trophyDao().findTrophiesForUser(user.get(0).id);
        TextView textView = findViewById(R.id.result);
        Toast.makeText(this, trophiesForUser.toString(), Toast.LENGTH_SHORT).show();
        if (user.size()>0){
            textView.setText(user.get(0).name + " Skill points " + user.get(0).skillPoints + " Trophys " + trophiesForUser.size() );
        }
    }

    public void onClick(View view){
        if (view.getId()==R.id.addtrophybutton) {
            // TODO add trophy
            // TODO call updatefirstUserData
            Toast.makeText(this,String.valueOf(user.id), Toast.LENGTH_SHORT).show();
            Trophy trophy = new Trophy(user.id, "More stuff");
            database.trophyDao().addTrophy(trophy);
        }
        if (view.getId()==R.id.increaseskills ){
            user.skillPoints++;
            database.userDao().updateUser(user);
            // TODO to skillpoints

        }
        // TODO call updatefirstUserData
        updateFirstUserData();

    }
    @Override
    protected void onDestroy() {
        AppDatabase.destroyInstance();
        super.onDestroy();
    }
}

3.3. Optional: Add another table to your application

Each user can have Problems. A problem can have a description and an number.

Change your code so that you can also persists a number of projects for a user. == Working directly with SQLite

While it is possible to work directly with SQLite, using Using Room as SQL object mapping library should be preferred. The usage of Room largely simplifies the handling of databases.

Using the database APIs as described in this section, provides several challenges:

  • The API is relatively low-level, and requires a large amount of development time and effort.

  • Raw SQL queries can not verified at compile-time.

  • A lot of boilerplate code is required to convert between SQL queries and data objects.

The android.database package contains all necessary classes for working with databases. The android.database.sqlite package contains the SQLite specific classes.

To create and upgrade a database in your Android application you subclass the SQLiteOpenHelper class. An SQLiteDatabase object is the Java representation of the database.

In the constructor of your subclass, you specify the the name and the version of your database in the SQLiteOpenHelper.super() call.

In your SQLiteOpenHelper subclass class you need to override the following methods:

  • onCreate() - is called by the framework, if the database is accessed but not yet created.

  • onUpgrade() - called, if the database version is increased in your application code. This method allows you to update an existing database schema or to drop the existing database and recreate it via the onCreate() method.

The SQLiteOpenHelper class provides the getReadableDatabase() and getWriteableDatabase() methods. They allow read or write access to the SQLiteDatabase database.

The database tables should use the identifier _id for the primary key of the table. Several Android functions rely on this standard.

SQLiteDatabase is the base class for working with a SQLite database. It provides the insert(), update() and delete() methods to open, query, update and close the database.== Performance

Changes in SQLite are ACID (atomic, consistent, isolated, durable). This means that every update, insert and delete operation is ACID. Unfortunately this requires some overhead in the database processing therefore you should wrap updates in the SQLite database in an transaction and commit this transaction after several operations. This can significantly improve performance.

The following code demonstrates that performance optimization.

db.beginTransaction();
try {
   for (int i= 0; i< values.lenght; i++){
       // TODO prepare ContentValues object values
       db.insert(your_table, null, values);
       // In case you do larger updates
       yieldIfContededSafely()
     }
     db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
}

For larger data updates you should use the yieldIfContededSafely() method. SQLite locks the database during an transaction. With this call, Android checks if someone else queries the data and if finish automatically the transaction and opens a new one. This way the other process can access the data in between.

It also provides the execSQL() method, which allows to execute SQL statements.

The object ContentValues allows to define key/values for inserts and updates. The key represents the table column identifier and the value represents the content for the table record in this column.

Queries can be created via the rawQuery() and query() methods. rawQuery() directly accepts an SQL select statement as input. query() provides a structured interface for specifying the SQL query.

SQLiteQueryBuilder is a convenience class that helps to build SQL queries.

The following listings demonstrate the usage of the different calls.

Cursor cursor = getReadableDatabase().
    rawQuery("select * from todo where _id = ?", new String[] { id });
return database.query(DATABASE_TABLE,
    new String[] { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION },
    null, null, null, null, null);

The method query() has the following parameters.

Table 1. Parameters of the query() method
Parameter Comment

String dbName

The table name to compile the query against.

String[] columnNames

A list of which table columns to return. Passing "null" will return all columns.

String whereClause

Where-clause, i.e. filter for the selection of data, null will select all data.

String[] selectionArgs

You may include ?s in the "whereClause"". These placeholders will get replaced by the values from the selectionArgs array.

String[] groupBy

A filter declaring how to group rows, null will cause the rows to not be grouped.

String[] having

Filter for the groups, null means no filter.

String[] orderBy

Table columns which will be used to order the data, null means no ordering.

If a condition is not required you can pass null, e.g. for the group by clause.

The "whereClause" is specified without the word "where", for example a "where" statement might look like: "_id=19 and summary=?".

If you specify placeholder values in the where clause via ?, you pass them as the selectionArgs parameter to the query.

A query returns a Cursor object. A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory.

To get the number of elements of the resulting query use the getCount() method.

To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method determines if the end of the query result has been reached.

Cursor provides typed get*() methods, e.g., getLong(columnIndex), getString(columnIndex) to access the column data of the current cursor.

Cursor also provides the getColumnIndexOrThrow(String) method which allows to get the column index for a column name of the table.

A Cursor needs to be closed with the close() method call.

3.4. Performance

You should wrap updates in the SQLite database in an transaction and commit this transaction after several operations. This can significantly improve performance.

The following code demonstrates that performance optimization.

db.beginTransaction();
try {
   for (int i= 0; i< values.lenght; i++){
       // TODO prepare ContentValues object values
       db.insert(your_table, null, values);
       // In case you do larger updates
       yieldIfContededSafely()
     }
     db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
}

For larger data updates you should use the yieldIfContededSafely() method. SQLite locks the database during an transaction. With this call, Android checks if someone else queries the data and if finish automatically the transaction and opens a new one. This way the other process can access the data in between.

4. Content provider and sharing data

4.1. What is a content provider?

Content provider can provide data to other applications or components. A provider must be declared in the manifest file for the application. Via the android:exported=false|true flag you can define if the provide is available for other applications or not.

Consumers access the data provided via URI’s. Any URI which starts with content:// points to a resources which can be accessed via a provider. A URI for a resource may allow to perform the basic CRUD operations (Create, Read, Update, Delete) on the resource via the content provider.

A provider allows applications to access data. The data can be stored in an SQlite database, on the file system, in flat files or on a remote server.

The base URI to access a content provider is defined via the combination of the content:// schema and the name space of the provider. This name space is defined in the manifest file via the android:authorities attribute of the receiver registration. For example: content://test/

The base URI represents a collection of resources. If the base URI is combined with an instance identifier, e,g., content://test/2, it represents a single instance.

It is good practice to provide public constants for the URIs to document them to other developers.

4.2. Custom content provider

To create your custom content provider you have to define a class which extends android.content.ContentProvider. You must declare this class as content provider in the Android manifest file. The corresponding entry must specify the android:authorities attribute which allows identifying the content provider. This authority is the basis for the URI to access data and must be unique.

<provider
       android:authorities="de.vogella.android.todos.contentprovider"
       android:name=".contentprovider.MyTodoContentProvider" >
</provider>

If a content provider does support methods its good practice to throw an UnsupportedOperationException().

A content provider can be accessed from several programs at the same time. Therefore, you must implement its methods thread-safe.

5. Inspecting the database

SQlite stores the whole database in a file on the Android device. On an emulator or a rooted devices you can access this file. You can pull the database on your computer to investigate the content via your favorite SQlite desktop client.

For this use the following command to connect to the device.

# root the device
adb root

# pull the `userdatabase.db` database from the app with
#
adb pull /data/data/com.vogella.android.persistence/databases/userdatabase.db
# Enter .dump to see all the tables

[TIP]
====
Open source projects like https://github.com/infinum/android_dbinspector allow you to view the data directly on the device.
====

The adb command line tool is located in your Android SDK installation folder in the platform-tools subfolder.

The most important commands for the sqlite command line access are:

Table 2. SQlite commands
Command Description

.help

List all commands and options.

.exit

Exit the sqlite3 command.

.schema

Show the CREATE statements which were used to create the tables of the current database.

You find the complete documentation of SQlite at http://www.sqlite.org/sqlite.html.

6. About this website

7. Android SQLite resources

7.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2017 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.