Hi guys im creating game and im new in sql lite. i have a code for inserting database but i dont know how to maximum it at 10 and also i dont know how to compare the new score of the player to the older scores. also how can i delete in the top 10 scored the lowest if the new score beat 1 of the top 10 score. im looking for great codes with good explanation so that i will understand the flow of codes.
Heres my database class:
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;
}
}
should i use method?