数据库最大值为10,并根据分数排序,如果分数最低,则删除分数的替换

时间:2013-02-05 04:26:37

标签: android sql sqlite

嗨,大家好我在sql lite中创建游戏和新手。我有一个插入数据库的代码,但我不知道如何最大限度地在10,我也不知道如何比较玩家的新分数和旧的分数。如果新分数击败前10名中的1分,我怎么能在前10名中删除得分最低。我正在寻找具有良好解释的优秀代码,以便我理解代码的流程。

继承我的数据库类:

        public class highscore {

 private static final String DATABASE_NAME = "topscoredb";
 private static final String DATABASE_TABLE = "tablescore";
 private static final int DATABASE_VERSION = 2;
 public static final String KEY_ROWID = "_id";
 public static final String KEY_SCORE = "score";
 public static final String KEY_NAME = "players_name";

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);
        // TODO Auto-generated constructor stub
    }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                     KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                     KEY_NAME + " TEXT NOT  NULL, " +
                     KEY_SCORE + " TEXT NOT NULL);"

                );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST" + DATABASE_TABLE);
            onCreate(db);

        }
 }



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

 public highscore open() throws SQLException{
 ourHelper = new DbHelper(ourContext);
 ourDatabase = ourHelper.getWritableDatabase();


  return this;

 }

  public void close(){
   ourHelper.close();

    }



   public long createEntry(String name, String tscore) {
// TODO Auto-generated method stub

  ContentValues cv = new ContentValues();
  cv.put(KEY_NAME, name);
  cv.put(KEY_SCORE, tscore);
 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, null);
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;
    }

    }

我应该使用方法吗?

2 个答案:

答案 0 :(得分:1)

好的android使用sqlite,您可以使用Limit an sqlite Table's Maximum Number of Rows中的解决方案。

您也可以按降序获得结果

ourDatabase.query(DATABASE_TABLE, columns, null, null, null,null,KEYSCORE + " DESC");

并显示前10名结果。

或使用SQlite查询构建器方法来限制结果 http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html#query(android.database.sqlite.SQLiteDatabase,java.lang.String [],java.lang.String,java.lang.String [],java.lang.String,java.lang.String,java.lang.String,java.lang。字符串)

答案 1 :(得分:-1)

我们可以将所有分数存储在数据库中并写一个写一个查询获得前十个最高分......

select * from(选择empno,sal,rank()over(sal desc的顺序)作为来自emp的rnk,其中rnk< = 5);