Gau*_*sth 29 android kotlin android-room
我正在使用带有kotlin的android持久性库房间.
Dao看起来像这样
@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
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很好,但我收到了这个错误
Error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :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();
}
}
Run Code Online (Sandbox Code Playgroud)
在这个方法中,我看到方法中的任何地方都没有使用arg0.
编辑:这似乎是在新的插件中修复.这里的几个答案是正确的,但我不能接受每一个答案,抱歉.
小智 62
Kotlin没有正确保留参数的名称 - 这是https://youtrack.jetbrains.com/issue/KT-17959
您可以通过在@Query语句中使用:arg0,:arg1等作为参数名称来解决此问题:
@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
Run Code Online (Sandbox Code Playgroud)
对于我来说,在项目中使用kotlin v.1.2.10及更高版本它只能通过下一个方式工作:
@Query("select * from user where pk = :userId")
fun findUserById(userId: Long): DBUser
Run Code Online (Sandbox Code Playgroud)
查询中的名称" :userId "和方法中的" userId "应该相同.
如果未apply plugin: 'kotlin-kapt'在build.gradle文件中定义,则会发生此错误。如果确实启用了kapt插件,则编译器将按预期将绑定参数与方法参数进行匹配。
对我来说,当我在DAO接口或数据库Entity类中更改/添加方法时,就会出现这个问题,我通过Build > Clean Project紧随其后解决了它Build > Make Project。然后运行它。
| 归档时间: |
|
| 查看次数: |
6152 次 |
| 最近记录: |