对于 Android 应用程序,如何按降序排列我的 SQLITE 数据库? [英] How do I order my SQLITE database in descending order, for an android app?

查看:29
本文介绍了对于 Android 应用程序,如何按降序排列我的 SQLITE 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按降序显示数据的最有效方法是什么?

What is the most efficient method of showing my data in descending order?

public String getRank() {

    String[] rank = new String[]{ KEY_ROWID };
    Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, null);   //reading information from db.
    String rankResult = "";

    int iRow = 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(iRow) + "
"; 
        //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, null);   //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) + "
"; 
        //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, null);   //reading information from db.
    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) + "
"; 
        //Returning value of row that it is currently on.
    }
    return scoreResult; //returning result
}

推荐答案

Query 有两种语法,你使用的语法,最后一列代表 orderBy,你只需要指定你要在哪一列上做 orderBy +"ASC"(或) orderBy +"DESC"

Query has two syntax, the syntax you are using, last column represents orderBy, you just need to specify on what column you want to do orderBy +"ASC" (or) orderBy +"DESC"

Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, yourColumn+" DESC"); 

参考 this 文档以了解有关 query() 方法的更多信息.

Refer this documentation to understand more about query() method.

这篇关于对于 Android 应用程序,如何按降序排列我的 SQLITE 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆