Sqlite Query for multiple values in one column

后端 未结 6 771
孤城傲影
孤城傲影 2020-12-30 14:07

I wanted to do query in table for field id with some vales like 1,5,4,11 which will come from previous screen according to selection.

cursor = database.query         


        
6条回答
  •  暖寄归人
    2020-12-30 14:32

    The correct syntax for using the IN operator in Android's ContentProvider is as follows:

    cursor = database.query(contentUri, projection, "columname IN(?,?)", new String[]{"value1" , "value2"}, sortOrder);
    

    Alternatively, we can also use,

    cursor = database.query(contentUri, projection, "columnName IN(?)", new String[] {" 'value1' , 'value2' "}, sortOrder);
    

    Note that we need single quotes around each comma-separated value in the arguments for second case, otherwise the whole string will be treated as one value for the column. The SQL will treat it as

    SELECT * FROM table WHERE columnName IN ('value1,value2')

    instead of the correct syntax

    SELECT * FROM table WHERE columnName IN ('value1' , 'value2')

提交回复
热议问题