如何使用rawquery方法在Android中使用SQLite对行进行排序?

时间:2011-11-14 14:49:09

标签: java android sqlite

我正在使用以下方法从表中获取数据:

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)

1 个答案:

答案 0 :(得分:18)

您在rawQuery方法上使用了第二个参数,并期望用作订单子句。事实并非如此。第二个参数用于为SQL查询字符串中的?占位符提供值。使用以下应该可以做到这一点:

Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
            + KEY_SPONSOR + " AS result FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_SWIMMERLAPS+" DESC", null);