Search code examples
sqliteandroid-sqliteandroid-room

sql lite update query list parameter issue


I have a simple sql lite query using room like this.

@Query("UPDATE MYTABLE SET CAT_ID = :idCata WHERE MYTABLE_ID IN (:listIds)")
int updateAll(long idCata, String listIds);

Where CAT_ID is defined as a nullable foreign key and MYTABLE_ID is primary key for MYTABLE.

All is working if i use one value in listIds parameter. If i put something like "1,5" this does not update the rows in the database. If i use "1" or "5" as single value in listIds, all is ok.

I have nothing in logcat or something like issue in logs.

Can someone explain me why ?

EDIT : I posted answer


Solution

  • Here is the solution, note i used long array because the query is updating foreign key values which cannot be typed as string. I think it is more elegant and safer. So the final code is :

    String[] ids = listIds.split(",");
    long[] idsForQuery = new long[ids.length];
    try{
        for(int i = 0; i < ids.length; i++){
            idsForQuery[i] = Long.parseLong(ids[i]);
        }
        mDao.updateAll(idCata, idsForQuery);
    }
    catch(NumberFormatException e){
        // something wrong with ids type !!
    }
    

    And in the dao, the sql lite query.

    @Query("UPDATE MYTABLE SET CAT_ID = :idCata WHERE MYTABLE_ID IN (:listIds)")
    int updateAll(long idCata, long[] listIds);
    

    Thanks to MikeT for the help.