Android SQLite ORDER BY不起作用

时间:2011-01-30 20:10:27

标签: android sql sqlite cursor

我有一个非常基本的情况:

我的桌子大约有5k行:

CREATE TABLE "words" ("id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "name" TEXT NOT NULL , "def" TEXT NOT NULL, "rand" INTEGER)

我使用“UPDATE words SET rand = random()”

定期更新

在android中,当我使用rawQuery()使用以下内容创建游标时:

SELECT w.id, w.name, w.def, w.rand FROM words w ORDER BY w.rand ASC;

返回的游标不按正确的顺序迭代。例如。它将按以下顺序输出具有rand值的列:

-1298882092
-2138143484
-1115732861
118839193
...

有谁知道这里发生了什么?这不应该工作吗?如果我在SQLiteManager中运行完全相同的查询,它会以正确的顺序返回结果,所以这似乎是特定于android / cursor。

更新:

以下是android中的代码,我尝试了多种方法:

尝试1:

Cursor cursor = db.rawQuery("SELECT w.id, w.name, w.def, w.rand FROM words w ORDER BY w.rand ASC", new String[]{});

尝试2:

Cursor cursor = db.query("words", new String[]{"id", "name", "def", "rand"},
            null, null, null, null, "rand ASC");

在这两种情况下,我都会按以下方式进行迭代:

while(cursor.moveToNext()) {
    ...
    Log.i("Test", cursor.getInt(3));
    ...
}

2 个答案:

答案 0 :(得分:6)

我解决了,是一个简单的疏忽。 random()函数返回的值可以大于java int数据类型。这产生溢出。切换到getLong(),一切正常。光标一直在正确迭代。

答案 1 :(得分:1)

我遇到了android游标,rawquery和group by的问题。结果没有订购。

这是表格:

public static final String I_ID = "iid";
public static final String I_QID = "qid";
public static final String I_STATUS = "status";
public static final String I_PDATE = "pub_date";
public static final String I_TYPE = "tipo";
public static final String I_TITLE = "titolo";
public static final String I_LINK = "link";
public static final String I_QR = "qrcode";
public static final String I_DESC = "descrizione";

private static final String C_ITEM_T = 
        "CREATE TABLE \""+T_ITEM+"\" ("+
        "\""+I_ID+"\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
        "\""+I_QID+"\" INTEGER NOT NULL REFERENCES "+T_QUERY+"("+Q_ID+") ,"+
        "\""+I_STATUS+"\" INTEGER NOT NULL,"+
        "\""+I_PDATE+"\" TEXT NOT NULL,"+
        "\""+I_TYPE+"\" TEXT NOT NULL,"+
        "\""+I_TITLE+"\" TEXT NOT NULL,"+
        "\""+I_LINK+"\" TEXT UNIQUE NOT NULL,"+
        "\""+I_QR+"\" TEXT UNIQUE NOT NULL,"+
        "\""+I_DESC+"\" TEXT"+
        ");";

public static final String A_ID = "aid";
public static final String A_IID = "iid";
public static final String A_SIZE = "grandezza";
public static final String A_DURATION = "durata";
public static final String A_CHANNELS = "canali";
public static final String A_RATE = "sampleRate";
public static final String A_TORRENT = "torrent";
public static final String A_DOWNLOAD = "download";
public static final String A_LICENSE_URL = "licenza_url";
public static final String A_TAGS = "tags";
public static final String A_CREATOR = "creatore";

private static final String C_ATTR_T = 
        "CREATE TABLE \""+T_ATTR+"\" ("+
        "\""+A_ID+"\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
        "\""+A_IID+"\" INTEGER NOT NULL REFERENCES "+T_ITEM+"("+I_ID+") ,"+
        "\""+A_SIZE+"\" LONG NOT NULL,"+
        "\""+A_DURATION+"\" LONG NOT NULL,"+
        "\""+A_CHANNELS+"\" TEXT NOT NULL,"+
        "\""+A_RATE+"\" TEXT NOT NULL,"+
        "\""+A_TORRENT+"\" TEXT NOT NULL,"+
        "\""+A_DOWNLOAD+"\" TEXT NOT NULL,"+
        "\""+A_LICENSE_URL+"\"TEXT,"+
        "\""+A_TAGS+"\" TEXT,"+
        "\""+A_CREATOR+"\" TEXT,"+
        "UNIQUE ("+A_ID+","+A_IID+")"+
        ");";

这是我的查询:

public static final String FILL_GRID = "SELECT * "+
        "FROM "+ Db.T_ITEM + " i " +
        "LEFT OUTER JOIN "+Db.T_ATTR+" a ON ( i."+Db.I_ID+"=a."+Db.A_IID+ ")"+ 
        " WHERE i."+Db.I_QID+" = ? AND i."+Db.I_STATUS+" <> "+Db.STATUS_DELETED +
                    " GROUP BY ? ";

以及我如何使用它:

Cursor c = db.rawQuery(FILL_GRID, new String[]{String.valueOf(qid), order});

我确定qid是一个整数,并命令i.titolo ASC或DESC

对我有用的唯一解决方法是修改在rawquery调用方法中通过clausole移动组的查询:

public static final String FILL_GRID = "SELECT * "+
        "FROM "+ Db.T_ITEM + " i " +
        "LEFT OUTER JOIN "+Db.T_ATTR+" a ON ( i."+Db.I_ID+"=a."+Db.A_IID+ ")"+ 
        " WHERE i."+Db.I_QID+" = ? AND i."+Db.I_STATUS+" <> "+Db.STATUS_DELETED;

Cursor c = db.rawQuery(FILL_GRID + " ORDER BY " + order, new String[]{String.valueOf(qid)});