如何按_id AND排序SQLite数据

时间:2013-03-08 17:10:29

标签: java android sqlite

我有这个数据库用于我的游戏结果,我希望它按降序时间排序,但是在左边我不想在图片中,我想从1到20。如何去做?我将所有这些数据放在我的xml文件中的TextView中。

screenshot

这是我的数据库类:

public class OffDBHelper {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_SCORE = "score";

    private static final String DATABASE_NAME = "highscores";
    private static final String DATABASE_TABLE = "highscorestable";
    public static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_NAME + " TEXT NOT NULL, " +
                    KEY_SCORE + " DOUBLE NOT NULL);"
                    );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);       
        }

    }

    public OffDBHelper(Context c){
        ourContext = c;
    }

    public OffDBHelper open() throws Exception{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        ourHelper.close();
    }

    public long createEntry(String name, double score) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_SCORE, score);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getData() {
        String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_SCORE};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, "score DESC");
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iScore = c.getColumnIndex(KEY_SCORE);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iScore) + "\n";
        }

        return result;


    }
}

我的数据管理课程:

TextView tv = (TextView) findViewById(R.id.tvSQLinfo);

        OffDBHelper info = new OffDBHelper(this);
        try {
            info.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String data = info.getData();
        info.close();
        tv.setText(data);

3 个答案:

答案 0 :(得分:3)

这就是你需要的

TableConstants.Reminders.COLUMN_ONE + " ASC, " + 
TableConstants.Reminders.COLUMN_ID + " DESC"

在你的情况下你需要修改

  Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, "_id ASC,  
    score DESC");

答案 1 :(得分:1)

  public String getData() {
        String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_SCORE};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, KEY_ROWID +" ASC");// REPLACE WITH ASC AS WELL AS ALSO PUT DEASC FOR DSCEDING
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iScore = c.getColumnIndex(KEY_SCORE);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iScore) + "\n";
        }

答案 2 :(得分:0)

在我的情况下是查询函数错误:

Cursor c = db.query(SEARCH_TABLE_NAME, null, null, null, null, null, null, SEARCH_COUNT + " DESC");

绝对:

Cursor c = db.query(SEARCH_TABLE_NAME, null, null, null, null, null, SEARCH_COUNT + " DESC");

1 arg,然后我需要......