Problem with SQL Query Android Where Clause

前端 未结 1 1558
心在旅途
心在旅途 2021-01-18 13:37

I have the following code that creates my table. I insert data into it that looks like this

_id     date     recordName     total
--------------------------         


        
1条回答
  •  清酒与你
    2021-01-18 14:08

    You need to put single quotes around the value, otherwise it will treat it as a column.

    KEY_RECORD_NAME + " = '" + name + "'"
    

    Note: This solution is open to Sql Injection, you should use parameter placeholders, and pass the values via the selectionArgs argument:

    public Cursor getRecord(String name) throws SQLException {
      return mDb.query(true, 
        DATABASE_TABLE_RECORDS, 
        new String[] {KEY_ROWID, KEY_DATE, KEY_RECORD_NAME, KEY_TOTAL}, 
        KEY_RECORD_NAME + " = ?",
        new String[] {name},
        null, null, null, null);
    }
    

    Alternatively, look into SQLiteQueryBuilder

    0 讨论(0)
提交回复
热议问题