Perform data updates via BriteDatabase
trunkBriteDatabase instance rather than the underlying SupportSQLiteDatabase.repository·trunk·Indexed 26 days ago
https://github.com/square/sqlbriteA 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.
BriteDatabase instance rather than the underlying SupportSQLiteDatabase.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'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();
}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());SQL Brite is no longer actively developed.
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(/*...*/);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...
}
});