Skip to content

Raw Query

Siim Kinks edited this page Nov 21, 2016 · 1 revision

If everything else fails, create a raw query. To create a raw query, call Select.raw(String) which returns a raw query builder object. It is also needed to provide which tables this query involves by invoking from builder method and provide at least one of annotation processor generated table objects.

Thanks to providing involved tables, SqliteMagic can notify hot RxJava queries when changes in those tables happen.

Like every other operation builder, raw query must end with an "executive" method - either execute() for synchronous execution or observe() for starting point into the reactive world. Executive methods produce Cursor objects.

Since its a raw query, SqliteMagic can't help with the object parsing, so it has to be done manually.

Cursor cursor = null
try {
  cursor = Select
          .raw("SELECT * " +
               "FROM BOOK " +
               "LEFT JOIN AUTHOR ON BOOK.AUTHOR = AUTHOR._ID " +
               "WHERE BOOK.TITLE LIKE ?")
          .from(BOOK, AUTHOR)
          .withArgs("%Foo%")
          .execute();
  while (cursor.moveToNext()) {
    // parse cursor row
  }
} finally {
  // don't forget to close the cursor!
  if (cursor != null) {
    cursor.close();
  }
}

See Next

Clone this wiki locally