简体   繁体   中英

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, TIMETABLE_LECTURER, TIMETABLE_TIME}, null, null, null, null, null);
}

For example if i wanted TIMETABLE_MODULECODE = 123. How would i do this, i have read its the first null. I have tried this below but still doesnt work

public Cursor fetchTimetable() {
    String a = "TIMETABLE_CODE = ADD";
    return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID, TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_ROOM, TIMETABLE_LECTURER, TIMETABLE_TIME}, a, null, null, null, null);
}

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
        );
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

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); 
} 

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); 
} 

简单的方法:

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

You can use Use this query:

SQLiteDatabase db = this.getReadableDatabase();

//Cursor cursorr = db.rawQuery(Query, null);

Cursor cursorr =  db.rawQuery("select * from " + DATABASE_TABLE_EHS + " where " + TASK_ID + "='" + taskid + "'" , null);

    if (cursorr.moveToFirst()) 
   {

        do {

            // your code like get columns

            }
         while (cursorr.moveToNext());
    }
}

如果要检查字符串值,则需要将字符串用引号引起来,如下所示:

dbHelper.COL_EMAIL +"='"+givenEmail+"'", 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM