SQLiteDatabase - How to use where clause?

前端 未结 7 761
情歌与酒
情歌与酒 2021-02-09 02:57
public Cursor fetchTimetable() {
    return mDb.query(DATABASE_TABLE_TIMETABLE, new String[] {TIMETABLE_ROWID,    TIMETABLE_MODULECODE, TIMETABLE_MODULENAME, TIMETABLE_R         


        
相关标签:
7条回答
  • 2021-02-09 03:08
    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

    0 讨论(0)
  • 2021-02-09 03:08

    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); 
    } 
    
    0 讨论(0)
  • 2021-02-09 03:13

    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); 
    } 
    
    0 讨论(0)
  • 2021-02-09 03:17

    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());
        }
    }
    
    0 讨论(0)
  • 2021-02-09 03:22

    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
            );
    
    0 讨论(0)
  • 2021-02-09 03:27

    If you're checking against a string value you'll want to wrap the string in quotes like below:

    dbHelper.COL_EMAIL +"='"+givenEmail+"'", 
    
    0 讨论(0)
提交回复
热议问题