0

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

截屏

这是我的数据库类:

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);
4

3 回答 3

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");
于 2013-03-08T17:12:37.010 回答
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";
        }
于 2013-03-08T17:46:18.397 回答
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 比我需要的多...

于 2015-11-21T23:34:56.220 回答