J'utilise la bibliothèque de persistance Android Room
avec Kotlin.
Le site Dao
ressemble à ceci
@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
}
Cela me semble bon mais j'obtiens cette erreur
Error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :countryId.
Je peux voir que le paramètre est nommé comme countryId
Alors, quel est le problème ?
FYI : Voici le code généré dans 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();
}
}
Dans cette méthode, je vois arg0
n'est utilisé nulle part dans la méthode.
EDIT : Cela semble être corrigé dans les nouveaux plugins. Plusieurs des réponses ici sont correctes, mais je ne peux pas accepter toutes les réponses, Désolé.
2 votes
Il s'agit d'un bug youtrack.jetbrains.com/issue/KT-18048 youtrack.jetbrains.com/issue/KT-17959 issuetracker.google.com/issues/62125909
0 votes
Comment avez-vous obtenu ce code généré ?