gpt4 book ai didi

Android Sqlite order by desc 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:20 25 4
gpt4 key购买 nike

我对使用 Sqlite 数据库编程的 Android 非常陌生。我想按 desc 排序我的游戏得分列。游戏得分列类型是真实的,当我尝试按 gpoint 和 desc 排序时无法正常工作。请给我一些建议。代码如下:

public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
String selectQuery = "SELECT * FROM " + TABLE_SCORES+" ORDER BY gpoint DESC";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
Score score = new Score();
score.setID(Integer.parseInt(cursor.getString(0)));
score.setPoint(cursor.getFloat(1));
score.setLetter(cursor.getString(3));
score.setDate(cursor.getString(2));
scoreList.add(0, score);
} while (cursor.moveToNext());
}

return scoreList;
}

我的测试插入是:

            Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());

Log.d("Insert: ", "Inserting ..");
db.addScoreToDB(new Score((float)9.4, "S",formattedDate));
db.addScoreToDB(new Score((float)7.5, "S",formattedDate));
db.addScoreToDB(new Score((float)6.6, "S",formattedDate));
db.addScoreToDB(new Score((float)8.8, "S",formattedDate));
db.addScoreToDB(new Score((float)9.7, "S",formattedDate));
db.addScoreToDB(new Score((float)11.1, "S",formattedDate));
db.addScoreToDB(new Score((float)2.1, "S",formattedDate));
db.addScoreToDB(new Score((float)10.1, "S",formattedDate));

结果是:

12-24 15:52:24.065: D/插入:(9901): 插入 ..12-24 15:52:24.290: D/Reading:(9901): 读取所有联系人..
编号:8,分数:10.1,字母:S
编号:6,分数:11.1,字母:S
编号:7,分数:2.1,字母:S
编号:3,分数:6.6,字母:S
编号:2,分数:7.5,字母:S
编号:4,分数:8.8,字母:S
编号:1,分数:9.4,字母:S
编号:5,分数:9.7,字母:S

我想要结果:2.1、6.6、7.5.....等等

最佳答案

i want result: 2.1, 6.6, 7.5..... so on

这是升序,不是降序。默认为升序,但您也可以指定 ORDER BY column ASC

Id: 8 ,Score: 10.1 ,Letter: S
Id: 6 ,Score: 11.1 ,Letter: S
Id: 7 ,Score: 2.1 ,Letter: S
Id: 3 ,Score: 6.6 ,Letter: S
Id: 2 ,Score: 7.5 ,Letter: S
Id: 4 ,Score: 8.8 ,Letter: S
Id: 1 ,Score: 9.4 ,Letter: S
Id: 5 ,Score: 9.7 ,Letter: S

这是一个升序,但按字典顺序(字母顺序)排序,而不是数字排序。如果您想要数字排序,请将 gpoint 列类型设置为 REAL 或将值转换为 REAL:

CREATE TABLE ... (... gpoint REAL ...)

SELECT ... ORDER BY CAST(gpoint AS REAL)

关于Android Sqlite order by desc 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com