2011-11-14 82 views
2

我用下面的方法來從表中獲取數據:如何使用rawquery方法在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不是字符串:

方法rawQuery(字符串,字符串[])在類型SQLiteDatabase不適用參數(字符串,字符串)

+0

在這裏發佈完整的錯誤/異常消息。 –

回答

17

您使用了rawQuery方法的第二個參數,並且期望將其用作訂單子句。事實並非如此。第二個參數用於爲SQL查詢字符串中的?佔位符提供值。使用以下應該做的伎倆:

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