我正在使用以下方法从表中获取数据:
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result FROM " + DATABASE_TABLE, null);
我想知道是否可以将其编辑为按KEY_SWIMMERLAPS 对结果进行排序。我认为以下内容可行:
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result FROM " + DATABASE_TABLE, KEY_SWIMMERLAPS + " DESC");
但是,我收到错误,因为KEY_SWIMMERLAPS不是字符串:
SQLiteDatabase类型中的方法rawQuery(String,String [])不适用于参数(String,String)
答案 0 :(得分:18)
您在rawQuery
方法上使用了第二个参数,并期望用作订单子句。事实并非如此。第二个参数用于为SQL查询字符串中的?
占位符提供值。使用以下应该可以做到这一点:
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_SWIMMERLAPS+" DESC", null);