SQL Brite Documentation

repository·trunk·Indexed 26 days ago

https://github.com/square/sqlbrite

A lightweight wrapper around SupportSQLiteOpenHelper and ContentResolver that introduces reactive stream semantics to SQLite queries using RxJava. Provides BriteDatabase for reactive queries and transactions, and BriteContentResolver for observing ContentProviders. Note: This library is deprecated; users are encouraged to migrate to SQLDelight or Copper.

Tokens
905
Snippets
5
Records
7
Agent score
39%

What's inside SQL Brite

  1. Perform data updates via BriteDatabase

    trunk
    To ensure that reactive queries are notified of changes, all insert, update, or delete operations must be performed through the BriteDatabase instance rather than the underlying SupportSQLiteDatabase.
  2. Install SQL Brite via Gradle

    trunk

    To use SQL Brite in your Android project, add the following dependency to your build.gradle file.

    For the standard Java library:

    implementation 'com.squareup.sqlbrite3:sqlbrite:3.2.0'

    For the Kotlin module which provides extension functions to Observable<Query>:

    implementation 'com.squareup.sqlbrite3:sqlbrite-kotlin:3.2.0'
    implementation 'com.squareup.sqlbrite3:sqlbrite:3.2.0'
  3. Use transactions to batch updates

    trunk

    To prevent large data changes from spamming subscribers with frequent notifications, wrap multiple operations in a Transaction. A single notification will be triggered once the transaction is successfully marked and ended.

    Transaction transaction = db.newTransaction();
    try {
      db.insert("users", SQLiteDatabase.CONFLICT_ABORT, createUser("jw", "Jake Wharton"));
      db.insert("users", SQLiteDatabase.CONFLICT_ABORT, createUser("mattp", "Matt Precious"));
      transaction.markSuccessful();
    } finally {
      transaction.end();
    }
  4. Initialize BriteDatabase

    trunk

    To use SQL Brite, you must first create a SqlBrite instance using its builder, then wrap a SupportSQLiteOpenHelper and a Scheduler to create a BriteDatabase instance.

    The Scheduler (e.g., Schedulers.io()) determines the thread on which query notifications are triggered, ensuring you can run queries without blocking the main thread.

    SqlBrite sqlBrite = new SqlBrite.Builder().build();
    BriteDatabase db = sqlBrite.wrapDatabaseHelper(openHelper, Schedulers.io());
  5. Observe ContentProviders with BriteContentResolver

    trunk

    You can use SQL Brite to observe queries on another app's ContentProvider by wrapping a ContentResolver with a BriteContentResolver instance.

    BriteContentResolver resolver = sqlBrite.wrapContentProvider(contentResolver, Schedulers.io());
    Observable<Query> query = resolver.createQuery(/*...*/);
  6. Create reactive queries with createQuery()

    trunk

    Use BriteDatabase.createQuery(String tables, String sql) to create an Observable<Query>.

    Unlike standard SQLite queries, this method takes a list of table names to monitor. When any of those tables are modified via the BriteDatabase instance, the Observable will emit a new Query object. You must call query.run() to obtain the Cursor.

    Observable<Query> users = db.createQuery("users", "SELECT * FROM users");
    users.subscribe(new Consumer<Query>() {
      @Override public void accept(Query query) {
        Cursor cursor = query.run();
        // TODO parse data...
      }
    });