I’m writing an Android application for RC Cars. It uses a SQLite database, and saves the model of the car, car name and other variables.
Here’s my code:
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_CARNAME, KEY_TRACKNAME, KEY_TRACKSIZE, KEY_TRACKTRACTION, KEY_TRACKSURFACE, KEY_BODYTYPE,
KEY_COMMENTS}, KEY_CARNAME + "=" + "'MTX-4'", null, null, null, null);
}
How can I display only the records that have a certain model of car named in the carname column? So far I can only filter the results by using 'MTX-4' or some other literal string. I need to be able to replace that with a string variable like String carName or something, so I can filter by a user-defined carname.
Replace the ‘MTX-4’ with “?” as a parameter variable. Then replace the selectionArgs parameter (null in your example) with your list of parameters, e.g. new String[]{“MTX-4”} or new String[]{carName}
The following snippet is equivalent to yours.