SQLiteDatabase - 如何使用where子句?

时间:2012-02-25 13:57:27

标签: android mysql sqlite

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

例如,如果我想要TIMETABLE_MODULECODE = 123.我将如何做到这一点,我已经读过第一个null。我在下面尝试了这个,但仍然无法正常工作

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

7 个答案:

答案 0 :(得分:10)

我在args的工作示例(使用它时更清楚):

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

答案 1 :(得分:6)

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,此处“=”是where子句

答案 2 :(得分:4)

究竟什么“仍然不起作用”是什么意思?

以下代码应该可以正常工作:

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

答案 3 :(得分:4)

正确的语法是

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

答案 4 :(得分:2)

简单的方法:

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

答案 5 :(得分:1)

您可以使用此查询:

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

答案 6 :(得分:1)

如果您正在检查字符串值,那么您希望将字符串换成如下所示的引号:

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