Android SQLiteException: bind or column index out of range problem

前端 未结 3 943
自闭症患者
自闭症患者 2021-02-12 23:44

In android I am using the following statement.

model = dataHelper.rawQuery(\"SELECT _id, engword, lower(engword) as letter FROM word WHERE letter >= \'a\' AND         


        
3条回答
  •  旧时难觅i
    2021-02-13 00:34

    If anyone is like me trying (and failing) to get this working with getContentResolver().query here how I managed it:

    *Updated thanks to comments from @CL and @Wolfram Rittmeyer, as they said this is the same as for rawQuery *

    Correct way:

      public static String SELECTION_LIKE_EMP_NAME = Columns.EMPLOYEE_NAME
                + " like ?";            
    
      Cursor c = context.getContentResolver().query(contentUri,
                    PROJECTION, SELECTION_LIKE_EMP_NAME, new String[] { "%" + query + "%" }, null);
    

    Previous answer that was open to SQL injection attack:

    public static String SELECTION_LIKE_EMP_NAME = Columns.EMPLOYEE_NAME
                + " like '%?%'";
    
    String selection = SELECTION_LIKE_EMP_NAME.replace("?", query);
    
    Cursor c = context.getContentResolver().query(contentUri,
                PROJECTION, selection, null, null);
    

提交回复
热议问题