-1
我已經設法讓我的SQLite數據庫以降序出現在評分列中,但事情是我認爲它是將列識別爲字符串而不是int,因爲它出現了順序如下:降序SQLite Integer而不是字符串
7,6,4,2,1,19,
,我不知道如何去改變它,即使我敢肯定它的東西很簡單。這裏是我當前的代碼:
public class CodeBreakerDB {
public static final String KEY_ROWID = "_id"; //Referencing a specific row in the database.
public static final String KEY_NAME = "player_name";
public static final String KEY_SCORE = "player_score";
private static final String DATABASE_NAME = "PlayerScoresdb"; //Labeling the database
private static final String DATABASE_TABLE = "playerTable"; //Labeling the table
private static final int DATABASE_VERSION = 1;
private DbHelper scoreHelper;
private final Context scoreContext;
private SQLiteDatabase scoreDb;
//databaseHelper to help set up database
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { //Method only called on first use to generate db.
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_SCORE + " INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //Method called after first use
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public CodeBreakerDB(Context c){
scoreContext = c;
}
public CodeBreakerDB open() throws SQLException{
scoreHelper = new DbHelper(scoreContext); //Gets aspects set up for the database including name and version.
scoreDb = scoreHelper.getWritableDatabase(); //Opening of database through scoreHelper.
return this;
}
public void close(){
scoreHelper.close(); //Closes the scoreHelper class.
}
public long createScoreEntry(String name, String score) { // Writing to the actual database.
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCORE, score);
return scoreDb.insert(DATABASE_TABLE, null, cv);
}
public String getRank() {
String[] rank = new String[]{ KEY_ROWID };
Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, KEY_SCORE+" DESC");
String rankResult = "";
int iRow2 = c.getColumnIndex(KEY_ROWID); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
rankResult = rankResult + c.getString(iRow2) + "\n";
//Returning value of row that it is currently on.
}
return rankResult; //returning result
}
public String getName() {
String[] name = new String[]{ KEY_NAME };
Cursor c = scoreDb.query(DATABASE_TABLE, name, null, null, null, null, KEY_SCORE+" DESC"); //reading information from db.
String nameResult = "";
int iRow1 = c.getColumnIndex(KEY_NAME); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
nameResult = nameResult + c.getString(iRow1) + "\n";
//Returning value of row that it is currently on.
}
return nameResult; //returning result
}
public String getScore() {
String[] score = new String[]{ KEY_SCORE };
Cursor c = scoreDb.query(DATABASE_TABLE, score, null, null, null, null, KEY_SCORE+" DESC");
String scoreResult = "";
int iRow2 = c.getColumnIndex(KEY_SCORE); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
scoreResult = scoreResult + c.getString(iRow2) + "\n";
//Returning value of row that it is currently on.
}
return scoreResult; //returning result
}
}
是我需要改變的代碼的唯一部分?當我嘗試它,它使模擬器中的應用程序崩潰 –
Carla Dessi,請檢查編輯部分的答案。 – Rakesh
@CarlaDessi是否爲你工作? – Rakesh