Android SQLite 带有 ORDER BY 的负值

发布于 2024-10-14 18:23:57 字数 1117 浏览 5 评论 0原文

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

我有一个大约有 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;

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

-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));
    ...
}

I have a very basic situation:

I have a table with around 5k rows:

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

Which I periodically update using "UPDATE words SET rand=random()"

In android, when I create a cursor using rawQuery() using the following:

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

The returned cursor does not iterate in the correct order. E.g. it will output columns with rand values in the following order:

-1298882092
-2138143484
-1115732861
118839193
...

Does anyone know whats going on here? Shouldn't this work? If I run the exact same query in SQLiteManager it returns the results in the correct order, so this seems to be android/cursor specific.

UPDATE:

Here is the code in android, I have tried multiple ways:

Attempt 1:

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

Attempt 2:

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

In both cases I iterate like the following:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2024-10-21 18:23:57

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

I solved it, was a simple oversight. The random() function returns a value which can be larger than a java int data type. This was producing overflow. Switched to getLong() and everything works fine. The cursor was iterating correctly all along.

习ぎ惯性依靠 2024-10-21 18:23:57

我在使用 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)});

I had a problem with android cursor, rawquery and group by. The result was not ordered.

This is the table:

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+")"+
        ");";

And this was mine query:

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 ? ";

and here how i use it:

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

Where i am sure that qid is an integer, and order i.titolo ASC or DESC

The only workaround that worked for me is modify the query moving the group by clausole in the rawquery call method:

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)});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文