Android SQLLiteDataBase 游标返回 0 行

发布于 2024-11-15 09:11:52 字数 1275 浏览 3 评论 0原文

在我的一生中,我无法让光标返回任何数据。我已经验证了他们的数据在数据库中并且我的插入工作正常。官方给出的错误是:

CursorIndexOutOfBoundsException:请求索引 0,大小为 0

顶级声明:

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
    this.db = this.DBHelper.getWritableDatabase();
}

我的函数:

public String getRandomEntry()
    {
    int rand;
    Random random = new Random();
    int numEntries = (int)this.getCount();

    if(numEntries == 0)
        return "ERROR: Database is empty.";
    rand = random.nextInt(numEntries);

    Cursor cursor = DBHelper.getWritableDatabase().rawQuery(
            "SELECT * FROM " + DATABASE_TABLE 
            + " WHERE " + COLUMN_A + " = " + 0, null);

    Log.i("numEntries", Integer.toString(numEntries));
    Log.i("rand", Integer.toString(rand));
    Log.i("cursor", Integer.toString(cursor.getCount()));

    cursor.moveToFirst();
    return cursor.getString(0);
}

我也尝试过这样抓取光标:

Cursor cursor = db.rawQuery(
            "SELECT * FROM " + DATABASE_TABLE 
            + " WHERE " + COLUMN_A + " = " + 0, null);

请给我你的想法!谢谢你!!

For the life of me, I can't get the cursor to return any data. I've verified that their is data in the database and my insertions are working. The official error is:

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Top level declarations:

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
    this.db = this.DBHelper.getWritableDatabase();
}

My function:

public String getRandomEntry()
    {
    int rand;
    Random random = new Random();
    int numEntries = (int)this.getCount();

    if(numEntries == 0)
        return "ERROR: Database is empty.";
    rand = random.nextInt(numEntries);

    Cursor cursor = DBHelper.getWritableDatabase().rawQuery(
            "SELECT * FROM " + DATABASE_TABLE 
            + " WHERE " + COLUMN_A + " = " + 0, null);

    Log.i("numEntries", Integer.toString(numEntries));
    Log.i("rand", Integer.toString(rand));
    Log.i("cursor", Integer.toString(cursor.getCount()));

    cursor.moveToFirst();
    return cursor.getString(0);
}

I've also tried grabbing the cursor as such:

Cursor cursor = db.rawQuery(
            "SELECT * FROM " + DATABASE_TABLE 
            + " WHERE " + COLUMN_A + " = " + 0, null);

Please give me your thoughts! Thank you!!

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

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

发布评论

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

评论(2

转身泪倾城 2024-11-22 09:11:52

这里似乎没有什么是随机的。看起来您每次都在寻找值为 0 的 column_a 。

我假设 column_a 没有任何值为 0 的内容。

Nothing seems random here at all. It appears that you are looking for column_a with a value of 0 every time.

I would assume that column_a has nothing with a value of 0.

蓝礼 2024-11-22 09:11:52

首先尝试以这种方式查询:

String args[] = new String[]{"0"};

Cursor cursor = db.rawQuery(
        "SELECT * FROM " + DATABASE_TABLE + " WHERE " + COLUMN_A + " = ?", args);

如果您查询返回行,这不能帮助使用外部工具检查您的数据库。

First of all try to query this way:

String args[] = new String[]{"0"};

Cursor cursor = db.rawQuery(
        "SELECT * FROM " + DATABASE_TABLE + " WHERE " + COLUMN_A + " = ?", args);

If this not helps check your db with external tool if you query return rows there.

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