-1

我已经设法让我的 SQLite 数据库按分数列按降序显示,但问题是我认为它将该列识别为字符串而不是 int,因为它按以下顺序出现:

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
}
} 
4

1 回答 1

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

}

您将 KEY_SCORE 作为字符串。注意score类型是字符串。

也许你应该使用这个

cv.put(KEY_SCORE, Integer.parseint(score));

编辑:

还要更改您在哪里提取值的代码。IE

 rankResult = rankResult + c.getString(iRow2) + "\n"; 

rankResult = rankResult + Integer.toString(c.getInt(iRow2)) + "\n"; 

还删除您的表并重新创建它。

于 2012-06-22T19:27:46.560 回答