Skip to content Skip to sidebar Skip to footer

SQLiteDatabase - How To Use Where Clause?

public Cursor fetchTimetable() { return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETA

Solution 1:

My working example with where args (it's more clear with using it):

        String [] settingsProjection = {
                DBContract.Settings._ID,
                DBContract.Settings.COLUMN_NAME_USER_ID,
                DBContract.Settings.COLUMN_NAME_AUTO_LOGIN
        };

        String whereClause = DBContract.Settings.COLUMN_NAME_USER_ID+"=?";
        String [] whereArgs = {userId.toString()};

        Cursor c = db.query(
                DBContract.Settings.TABLE_NAME,
                settingsProjection,
                whereClause,
                whereArgs,
                null,
                null,
                null
        );

Solution 2:

db.query
        (
                TABLE_NAME,
                new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
                TABLE_ROW_ID + "=" + rowID,
                null, null, null, null, null
        );

TABLE_ROW_ID + "=" + rowID, here "=" is the where clause


Solution 3:

What exactly does "still doesn't work" mean?

The following code should work just fine:

public Cursor fetchTimetable() { 
    String[] columnNames = new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME};
    String whereClause = "TIMETABLE_MODULECODE=123"; 

    return mDb.query(DATABASE_TABLE_TIMETABLE, columnNames, whereClause, null, null, null, null); 
} 

Solution 4:

the right syntax is

String[] projection= {column names};
String[] where={"values for where clause"}; //this must be array     
public Cursor fetchTimetable() { 

    return mDb.query(TABLE_NAME, projrction, "columname"+"=?", where, null, null, null); 
} 

Solution 5:

The simple way:

return mDb.rawQuery("SELECT * FROM myTable WHERE column1 = "+ someValue, null);

Post a Comment for "SQLiteDatabase - How To Use Where Clause?"