Wednesday, 15 August 2012

java - SQLite db high scores table not inserting and sorting correctly - Android -



java - SQLite db high scores table not inserting and sorting correctly - Android -

my db keeps top 10 records. when db empty, db records scores , inserts them correctly way until there 10 records. then, example, when db has 10 scores, , user gets score should new #1 score, replaces current #1 score new 1 instead of inserting , bumping below downwards 1 , deleting bottom score. explained if not comment , seek clarify further.

my question how can high scores perform correctly in when high scores total 10 records already, new record inserted right spot , rest below moved downwards 1 lastly record getting bumped off (since new #11).

databasehelper.java

public class databasehelper extends sqliteopenhelper { sqlitedatabase db; private static final int database_version = 6; private static final string db_name = "test3"; private static final string db_path = "/data/data/matt.lyons.bibletrivia/databases/"; private static final string table = "highscoreslist"; // table columns names. private static final string rank = "_id"; private static final string score = "score"; private static final string percentage = "percentage"; private static final string total_score = "total_score"; private static final string category = "category"; //constructor databasehelper. public databasehelper(context context) { super(context, db_name, null, database_version); } //open db editable. public sqlitedatabase opendb() { db = this.getwritabledatabase(); homecoming db; } //delete selected 1 row. public void delete(long lowscore) { lowscore = getlowest(); db.delete(table, total_score + "=" + lowscore, null); } //sort rows in order of total_score column. public long getlowest() { cursor c = db.rawquery("select * " + table + " order " + total_score, null); long count = c.getcount(); long lowscore = -1; if(count == 10) { c.movetolast(); lowscore = c.getint(c.getcolumnindex(total_score)); } homecoming lowscore; } //calculate total_score based on score , percentage of game. public long calculatetotalscore(long score, int percentage) { long i; homecoming = (percentage * 1000) + score; } //check if new record makes top 10. public long check(long score, int percentage, long sum) { cursor c = db.rawquery("select " + total_score + " " + table, null); long count = c.getcount(); long low_score; if(count == 10) { c.movetolast(); low_score = c.getint(c.getcolumnindex(total_score)); homecoming low_score; } else { homecoming count; } } //insert new record. public long insert(long score, int percentage, long total_score, string category) { contentvalues values = new contentvalues(); values.put(score, score); values.put(percentage, percentage); values.put(total_score, total_score); values.put(category, category); homecoming db.insert(table, null, values); } //create table , columns. public void oncreate(sqlitedatabase db) { db.execsql("create table " + table + " (" + rank + " integer primary key autoincrement," + score + " long," + percentage + " integer," + category + " string," + total_score + " long" + ");"); } }

results.java

public class results extends activity { databasehelper dh; public void oncreate(bundle savedinstancestate) { dh = new databasehelper(this); dh.opendb(); showresults(); } public void showresults() { total_score = dh.calculatetotalscore(score, percentage); if(dh.getlowest() == -1) { dh.insert(score, percentage, total_score, category); } else { dh.delete(dh.getlowest()); dh.insert(score, percentage, total_score, category); } } }

why not just:

"select limit 10 * " + table + " order total_score desc"

that way you'd top 10 scores without having insert/delete time?

java android sqlite sorting insert

No comments:

Post a Comment