I am trying to query data with Androids persistence library Room from a existing Database. No compilation error or runtime error. There is just no result in the cursor.
The query is checked and looks fine (also in the generated Java as mentioned in the answer of 'Pinakin' Android Room Database DAO debug log
The database existis in the /data/data/com.project/databases/ folder. The database has the correct size but there are no other files such as mentioned in the answer of 'kosas' Android Room database file is empty
Similar style of copying as shown here https://android.jlelse.eu/room-persistence-library-with-pre-populated-database-5f17ef103d3d. I only changed the constructor to:
grammarDatabase = GrammarDatabase.getInstance(appContext);
because my GrammarDatabase looks like this:
@Database(
entities = {
Term.class
}, version = 1, exportSchema = false
)
public abstract class GrammarDatabase extends RoomDatabase {
private static final String DB_NAME = "database.db";
private static volatile GrammarDatabase instance;
public static synchronized GrammarDatabase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
private static GrammarDatabase create(final Context context) {
return Room.databaseBuilder(context, GrammarDatabase.class, DB_NAME).fallbackToDestructiveMigration().allowMainThreadQueries().build();
}
public abstract TermDao getTermDao();
}
The .fallbackToDestructiveMigration()
and .allowMainThreadQueries
is just for testing purposes.
The interface 'TermDao_Impl' (query string shortened):
@Override
public Cursor getSearchContent(String searchTerm, String languageCode) {
final String _sql = "SELECT T._id, T.term FROM term AS T INNER JOIN ... INNER JOIN language AS L INNER JOIN ... AND L.code= ? AND T.term LIKE ?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 2);
int _argIndex = 1;
if (languageCode == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, languageCode);
}
_argIndex = 2;
if (searchTerm == null) {
_statement.bindNull(_argIndex);
} else {
_statement.bindString(_argIndex, searchTerm);
}
final Cursor _tmpResult = __db.query(_statement);
return _tmpResult;
}
Call in MainActivity:
First: database = DatabaseHelper.getInstance(context).getGrammarDatabase();
Then:
Cursor c = database.getTermDao().getSearchContent("'"+searchTerm+"'", "'"+currentLanguageCode+"'");
if (c.moveToNext()) {
System.out.println("Found!");
System.out.println(c.getString(c.getColumnIndex("T.term")));
}
Tried to call with " ' " around the arguments and without.
So the cursor is empty eventhough the query will have a result (tested in DB Browser for SQLite). "Found!" Is not printed.