-
- @Override
- public void onCreate(SQLiteDatabase db) {
- Log.d("db", "onCreate called");
- onUpgrade(db, -1, LATEST_REVISION);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.d("db", "onUpgrade called");
- for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
- }
-
- private void applyRevision(SQLiteDatabase db, int rev_no) {
- final Resources rm = mContext.getResources();
- String rev_file = String.format(Locale.US, "sql_%d", rev_no);
-
- int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
- if (res_id == 0)
- throw new SQLException(String.format(Locale.US, "No resource for revision %d", rev_no));
- db.beginTransaction();
- try (InputStream res = rm.openRawResource(res_id)) {
- Log.d("db", "Applying revision " + String.valueOf(rev_no));
- InputStreamReader isr = new InputStreamReader(res);
- BufferedReader reader = new BufferedReader(isr);
-
- String line;
- int line_no = 1;
- while ((line = reader.readLine()) != null) {
- if (line.startsWith("--")) {
- line_no++;
- continue;
- }
- if (line.isEmpty()) {
- line_no++;
- continue;
- }
- try {
- db.execSQL(line);
+ public static void queryInBackground(@NonNull String statement, String[] params,
+ @NonNull final CallbackHelper callbackHelper) {
+ /* All callbacks are called in the new (asynchronous) thread! */
+ Thread t = new Thread(() -> {
+ callbackHelper.onStart();
+ try {
+ SQLiteDatabase db = App.getDatabase();
+
+ try (Cursor cursor = db.rawQuery(statement, params)) {
+ boolean gotRow = false;
+ while (cursor.moveToNext()) {
+ gotRow = true;
+ if (!callbackHelper.onRow(cursor))
+ break;
+ }
+ if (!gotRow) {
+ callbackHelper.onNoRows();
+ }