安卓机房 : Each bind variable in the query must have a matching method

标签 android kotlin android-room

我正在使用带有 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/

相关文章:

android - 以编程方式检索 Android API 版本

android - 短信电话号码验证的替代方案 - 全局

android - google-services.json 文件丢失

while-loop - 是否可以在 Kotlin 的 while 条件体中初始化一个变量?

android - Kotlin .let {} 空安全

android - 房间 : related entities - usable public constructor

android - 如何使用 Circle Ci 在电子邮件中获取 Android APK 安装链接

android - 我正在尝试将 Android 旧库升级到 androidx (Android + Kotlin)

安卓房间: Can calling value on LiveData be used to return plain data?

java - Android Room 插入所有问题