Mun*_*l L 3 sqlite android android-sqlite
我对使用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/Insert:(9901):插入.. 12-24 15:52:24.290:D /阅读:(9901):阅读所有联系人..
 
Id:8,得分:10.1,信:S
 
Id:6,得分:11.1,信:S
 
Id:7,得分:2.1,信:S
 
Id:3,得分:6.6,信:S
 
Id:2,得分:7.5,信:S
 
Id:4 ,得分:8.8,信:S
 
Id:1,得分:9.4,字母:S
 
Id:5,得分:9.7,信:S
我想要的结果:2.1,6.6,7.5 .....等等
我想要的结果:2.1,6.6,7.5 .....等等
这是一个升序,而不是下降.升序是默认值,但您也可以指定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)
| 归档时间: | 
 | 
| 查看次数: | 10605 次 | 
| 最近记录: |