Skip to content Skip to sidebar Skip to footer

Problem With Sql Query Android Where Clause

I have the following code that creates my table. I insert data into it that looks like this _id date recordName total -------------------------------------- 7 201

Solution 1:

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, 
    newString[] {KEY_ROWID, KEY_DATE, KEY_RECORD_NAME, KEY_TOTAL}, 
    KEY_RECORD_NAME + " = ?",
    newString[] {name},
    null, null, null, null);
}

Alternatively, look into SQLiteQueryBuilder

Post a Comment for "Problem With Sql Query Android Where Clause"