我正在尝试使用现有数据库中的Android持久性库Room查询数据。没有编译错误或运行时错误。光标中没有任何结果。
查询被检查并且看起来不错(同样在生成的Java中,如“ Pinakin” Android Room Database DAO debug log的答案中所述
数据库存在于/data/data/com.project/databases/文件夹中。 数据库大小正确,但是没有其他文件,例如'kosas'Android Room database file is empty
的答案中提到的文件类似的复制样式,如此处https://android.jlelse.eu/room-persistence-library-with-pre-populated-database-5f17ef103d3d所示。我只将构造函数更改为:
grammarDatabase = GrammarDatabase.getInstance(appContext);
因为我的GrammarDatabase看起来像这样:
@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();
}
.fallbackToDestructiveMigration()
和.allowMainThreadQueries
仅用于测试目的。
接口'TermDao_Impl'(查询字符串已缩短):
@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;
}
调用MainActivity:
第一位:database = DatabaseHelper.getInstance(context).getGrammarDatabase();
然后:
Cursor c = database.getTermDao().getSearchContent("'"+searchTerm+"'", "'"+currentLanguageCode+"'");
if (c.moveToNext()) {
System.out.println("Found!");
System.out.println(c.getString(c.getColumnIndex("T.term")));
}
试图在参数周围使用“”进行调用,而没有使用。
因此,即使查询将得到结果,游标也是空的(已在DB Browser中测试SQLite)。 “找到了!”不打印。