无法按降序显示sqlite数据库的值

时间:2012-09-13 06:33:00

标签: android sqlite android-listview

我试图在列表视图中以降序显示我的测验应用程序的分数。出于某种原因,它不会这样做。它显示了分数但不是降序。这是我的DBAdapter片段,用于获取分数的所有信息:

public Cursor getAllInfoScores()
             {
                return db.query(DATABASE_TABLE_HIGHSCORES, new String[] {ROW_ID, KEY_NAME, KEY_SCORES, KEY_DIFFICULTY},null,null,null,null, "scores DESC");

             }

我也试过这个:

 public Cursor getAllInfoScores()
             {
                return db.query(DATABASE_TABLE_HIGHSCORES, new String[] {"_id", "name", "scores", "difficulty"},null,null,null,null, "scores DESC");

             }

以下是我尝试将其显示在列表中的活动:

public class HighScores extends ListActivity{

DBAdapter db = new DBAdapter(this);
private Cursor mCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscores);

    db.open();
    fillData();

}

private void fillData() {
    mCursor = db.getAllInfoScores();
    String[] from = new String[]{DBAdapter.KEY_NAME, DBAdapter.KEY_SCORES, DBAdapter.KEY_DIFFICULTY}; 
    int[] to = new int[]{R.id.textViewnamehs, R.id.textViewscorehs, R.id.textViewdiff};
   SimpleCursorAdapter c = 
            new SimpleCursorAdapter(this, R.layout.highscoresrow, mCursor, from, to);
setListAdapter(c);


}

ERROR:

09-13 15:19:04.533: E/AndroidRuntime(12792): FATAL EXCEPTION: main
09-13 15:19:04.533: E/AndroidRuntime(12792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dtan.mathwiz/com.dtan.mathwiz.HighScores}: android.database.sqlite.SQLiteException: near "scores": syntax error: , while compiling: select _id,name,scores,difficulty from highscores orderby scores desc
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.os.Looper.loop(Looper.java:123)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread.main(ActivityThread.java:3683)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at java.lang.reflect.Method.invokeNative(Native Method)
 09-13 15:19:04.533: E/AndroidRuntime(12792):   at java.lang.reflect.Method.invoke(Method.java:507)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at dalvik.system.NativeStart.main(Native Method)
09-13 15:19:04.533: E/AndroidRuntime(12792): Caused by: android.database.sqlite.SQLiteException: near "scores": syntax error: , while compiling: select _id,name,scores,difficulty from highscores orderby scores desc
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
0 9-13 15:19:04.533: E/AndroidRuntime(12792):   at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at com.dtan.mathwiz.adapter.DBAdapter.getAllInfoScores(DBAdapter.java:138)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at com.dtan.mathwiz.HighScores.fillData(HighScores.java:29)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at com.dtan.mathwiz.HighScores.onCreate(HighScores.java:24)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-13 15:19:04.533: E/AndroidRuntime(12792):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-13 15:19:04.533: E/AndroidRuntime(12792):    ... 11 more

2 个答案:

答案 0 :(得分:1)

在查看您的查询时,您似乎缺少撰写KEYWORD ORDER BY

应该是

ORDER BYorder by

而不是

orderby

答案 1 :(得分:-1)

您必须包含ORDER这里是您可以使用的简单原始查询 db.rawQuery("SELECT _id,name,scores,difficulty from YourTableName order scores desc")