SQLiteOpenHelper的轻量级封装:SQLBrite

jopen 10年前

SQLBrite是SQLiteOpenHelper的轻量级封装。引入反应流语义SQL操作。

用法

Wrap aSQLiteOpenHelperinstance withSqlBrite:

SqlBrite db = SqlBrite.create(helper);

TheSqlBrite.createQuerymethod is similar toSQLiteOpenHelper.rawQueryexcept it takes an additional parameter of table(s) on which to listen for changes. Subscribe to the returnedObservable<Query>which will immediately notify with aQueryto run.

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

https://github.com/square/sqlbriteUnlike a traditionalrawQuery, updates to the specified table(s) will trigger additional notifications for as long as you remain subscribed to the observable. This means that when you insert, update, or delete data, any subscribed queries will update with the new data instantly.
final AtomicInteger queries = new AtomicInteger();  users.subscribe(new Action1<Query>() {    @Override public void call(Query query) {      queries.getAndIncrement();    }  });  System.out.println("Queries: " + queries.get()); // Prints 1    db.insert("users", createUser("jw", "Jake Wharton"));  db.insert("users", createUser("mattp", "Matt Precious"));  db.insert("users", createUser("strong", "Alec Strong"));    System.out.println("Queries: " + queries.get()); // Prints 4

项目主页:http://www.open-open.com/lib/view/home/1424601406888