降序SQLite Integer而不是String

时间:2012-06-22 19:15:09

标签: android database sqlite cursor

我设法让我的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
}
} 

1 个答案:

答案 0 :(得分: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类型是String。

可能你应该使用这个

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

编辑:

还要更改提取值的代码。即

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

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

也会删除你的表并重新创建它。