我正在使用带有 Kotlin 的 android 持久性库 Room
。
道
是这样的
@Dao
interface CountryDao {
@Query("SELECT * FROM countries")
fun loadAllCountried() : LiveData<List<CountryEntity>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(products: List<CountryEntity>)
@Query("SELECT * FROM countries WHERE id = :countryId")
fun loadCountry(countryId: Int): LiveData<CountryEntity>
@Query("SELECT * FROM countries WHERE id = :countryId")
fun loadCountrySync(countryId: Int): CountryEntity
}
这对我来说似乎不错,但我收到了这个错误
错误:查询中的每个绑定(bind)变量都必须有一个匹配的方法参数。找不到 :countryId 的方法参数。
我可以看到参数被命名为countryId
,那么可能是什么问题?
仅供引用: 这是CountryDao_Impl.java中生成的代码
@Override
public CountryEntity loadCountrySync(int arg0) {
final String _sql = "SELECT * FROM countries WHERE id = ?";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
int _argIndex = 1;
final Cursor _cursor = __db.query(_statement);
try {
final int _cursorIndexOfId = _cursor.getColumnIndexOrThrow("id");
final int _cursorIndexOfPopulation = _cursor.getColumnIndexOrThrow("population");
final CountryEntity _result;
if(_cursor.moveToFirst()) {
_result = new CountryEntity();
final int _tmpId;
_tmpId = _cursor.getInt(_cursorIndexOfId);
_result.setId(_tmpId);
final long _tmpPopulation;
_tmpPopulation = _cursor.getLong(_cursorIndexOfPopulation);
_result.setPopulation(_tmpPopulation);
} else {
_result = null;
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
在这个方法中,我看到 arg0
在方法的任何地方都没有使用。
编辑: 这似乎已在新插件中修复。这里有几个答案是正确的,但我不能接受每一个答案,对不起。
最佳答案
Kotlin 没有正确保留参数的名称 - 这是 https://youtrack.jetbrains.com/issue/KT-17959
您可以通过使用 :arg0
、:arg1
等作为 @Query 语句中的参数名称来解决此问题:
@Query("SELECT * FROM countries WHERE id = :arg0")
fun loadCountry(countryId: Int): LiveData<CountryEntity>
@Query("SELECT * FROM countries WHERE id = :arg0")
fun loadCountrySync(countryId: Int): CountryEntity
关于安卓机房 : Each bind variable in the query must have a matching method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206745/