android - 光标找不到列?

标签 android sqlite

我试图从表中加载值列表,但游标找不到列? 有问题的代码:

public final AccessToken[] fetchAccessTokenByServerId(final long serverId) {
    final Cursor c = db.query(
            ACCESS_TOKEN_TABLE,
            new String[] { ACCESS_TOKEN_COL_ID, ACCESS_TOKEN_COL_VALUE },
            ACCESS_TOKEN_COL_SERVER_ID + "=?",
            new String[] { Long.toString(serverId) },
            null,
            null,
            null);

    AccessToken[] result = new AccessToken[c.getCount()];
    for (int i = 0; i < result.length; i++) {
        long id = c.getLong(c.getColumnIndexOrThrow(ACCESS_TOKEN_COL_ID));
        String value = c.getString(c.getColumnIndexOrThrow(ACCESS_TOKEN_COL_VALUE));

        result[i] = new AccessToken(value, id, serverId);
        c.moveToNext();
    }
    return result;
}

public static final String COL_ID = "_id";
public static final String ACCESS_TOKEN_TABLE = "accesstoken";
public static final String ACCESS_TOKEN_COL_ID = COL_ID;
public static final String ACCESS_TOKEN_COL_SERVER_ID = "server_id";
public static final String ACCESS_TOKEN_COL_VALUE = "value";

但是 for 循环中的前两个语句失败了:

Index -1 is requested, with a size of 1

该表包含以下列:_id (= int)、server_id (= int) 和 value (= string)

只有一个条目:1, 1, test

函数的参数为​​1。

真诚的 徐子泽

最佳答案

在初始化你的游标之后,执行以下操作:

c.moveToFirst();

关于android - 光标找不到列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118335/

相关文章:

SQL 约束检查是否在另一个表中的条目

QtWebKit - 如何显示数据库中的二进制图像?

java - 可视化 sqlite 数据库模式

android - 如何使用 PDF 库在 android 应用程序中呈现 pdf

java - 如何在android 10中获取图像的方向信息?

android - 为什么我在 Flutter AnimatedChecked 中得到了这一点? (自定义油漆)

mysql - 我可以开始记录应用程序发起的 SQL 查询,以便了解它在做什么吗?

android - 房间内完全一对一的关系

android - 带有 WebView 的 ViewPager 允许滑动和不允许滚动 WebView

android - AWS MobileHub : Rename Android/iOS sample project