一、查询
1.创建SqlBrite
SqlBrite sqlBrite = SqlBrite.create();2.将SQLiteOpenHelper的一个实例和一个Scheduler作为参数创建BriteDatabase,正确地传入Scheduler可以防止线程被阻塞
BriteDatabase db = sqlBrite.wrapDatabaseHelper(openHelper, Schedulers.io());3.通过BriteDatabase.createQuery方法来进行查询,它需要两个参数,一个是所要查询的表,一个是sql语句,这个方法与SQLiteDatabase.rawQuery类似 Observable<Query> users = db.createQuery("users", "SELECT * FROM users");
4.通过返回的Observable来进行订阅,并通过query.run()方法获取数据
users.subscribe(new Action1<Query>() { @Override public void call(Query query) { Cursor cursor = query.run(); // TODO parse data... } });
二、增加、删除、更新表后所有订阅的数据都会马上进行更新
所有的增、删、改、查操作都必须通过对象BriteDatabase来正确地通知订阅者
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
三、
Unsubscribe from the returned
Subscription
to stop getting updates.
Unsubscribe from the returned Subscription
to stop getting updates.
final AtomicInteger queries = new AtomicInteger(); Subscription s = 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")); s.unsubscribe(); db.insert("users", createUser("strong", "Alec Strong")); System.out.println("Queries: " + queries.get()); // Prints 3
四、数据改变较大时使用transactions
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 Transaction transaction = db.newTransaction(); try { db.insert("users", createUser("jw", "Jake Wharton")); db.insert("users", createUser("mattp", "Matt Precious")); db.insert("users", createUser("strong", "Alec Strong")); transaction.markSuccessful(); } finally { transaction.end(); } System.out.println("Queries: " + queries.get()); // Prints 2
五、使用操作符
queries只是普通的RxJava 的Observable对象,所有的操作符都可以用来控制所要发给订阅者的数据
users.debounce(500, MILLISECONDS).subscribe(new Action1<Query>() { @Override public void call(Query query) { // TODO... } });
六、监测从其它app查询的数据
BriteContentResolver resolver = sqlBrite.wrapContentProvider(contentResolver, Schedulers.io()); Observable<Query> query = resolver.createQuery(/*...*/);
创建数据库和表的类:
public class RxDatabaseHelper extends SQLiteOpenHelper{ private String TAG = "RX"; private Context context; private static RxDatabaseHelper rxDatabaseHelper = null; private RxDatabaseHelper(Context context) { super(context, "rxdb.sqlite", null, 1); //创建数据库rxdb.sqlite this.context = context; } public static RxDatabaseHelper getInstance(Context context){ if(rxDatabaseHelper == null){ synchronized (RxDatabaseHelper.class){ if(rxDatabaseHelper == null){ rxDatabaseHelper = new RxDatabaseHelper(context); } } } return rxDatabaseHelper; } @Override public void onCreate(SQLiteDatabase db) { //创建表rx_db_test,表中的字段:_id,name,gender,age db.execSQL("create table if not exists rx_db_test (_id integer primary key,name text, gender text, age long)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { }
测试增删改查的类:
public class RxDatabaseActivity extends Activity{ private String TAG = "RX"; private RxDatabaseHelper rxDatabaseHelper; private SQLiteOpenHelper sqLiteOpenHelper; private SQLiteDatabase sqLiteDatabase; private BriteDatabase briteDatabase; private SqlBrite sqlBrite; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rx_widget_layout); sqLiteOpenHelper = RxDatabaseHelper.getInstance(getApplicationContext()); sqLiteDatabase = sqLiteOpenHelper.getWritableDatabase(); sqlBrite = SqlBrite.create(); briteDatabase = sqlBrite.wrapDatabaseHelper(sqLiteOpenHelper,Schedulers.io()); // testInser(); //增 // testDelete(); //删 // testUpdate(); //改 testQuery(); //查 } //查询数据 private void testQuery() { Observable<SqlBrite.Query> rxDbTest = briteDatabase.createQuery("rx_db_test", "SELECT * FROM rx_db_test"); rxDbTest.subscribe(new Action1<SqlBrite.Query>() { @Override public void call(SqlBrite.Query query) { Cursor cursor = query.run(); while (cursor.moveToNext()){ Log.e(TAG,"["+cursor.getString(cursor.getColumnIndex("name"))+","+cursor.getString(cursor.getColumnIndex("gender"))+"," +cursor.getLong(cursor.getColumnIndex("age"))+"]"); } } }); } //修改数据 private void testUpdate() { ContentValues contentValues = new ContentValues(); contentValues.put("age",10); //将姓名为zhangsan19的人的年龄改为10 briteDatabase.update("rx_db_test",contentValues,"name=?","zhangsan19"); } //删除数据 private void testDelete() { //删除age=10的那条数据 //第一个参数是表名,第二个参数是所要匹配的字段的名字,第三个参数是所要匹配的字段值 briteDatabase.delete("rx_db_test","age=?","10"); } //添加数据 private void testInser() { ContentValues contentValues = new ContentValues(); //第一个参数是表名,第二个参数是所要添加的数据的键值对 // db.execSQL("create table if not exists rx_db_test (_id integer primary key,name text, gender text, age long)"); for(int i=1;i<20;i++){ contentValues.put("name","zhangsan"+i); if(i%2 == 0) { contentValues.put("gender", "女"); }else{ contentValues.put("gender", "男"); } contentValues.put("age", i); briteDatabase.insert("rx_db_test", contentValues); } } }
增加数据运行结果:
删除数据运行结果(将age=10的那条数据删除了):
修改数据后运行的结果(将姓名为zhangsan19的人的年龄改为10):
查询运行的结果:
注:1.需要在build.gradle中添加compile 'com.squareup.sqlbrite:sqlbrite:0.7.0'
2.参考资料:https://github.com/ReactiveX/RxAndroid/wiki#removed
https://github.com/square/sqlbrite