rawQuery() 游标上对字段槽 0,1 的错误请求

发布于 2024-12-08 05:33:19 字数 1090 浏览 0 评论 0原文

我对 android 还很陌生,我遇到了以下问题。 这是我的代码,我试图将数据库中的数据获取到我的列表中。

private List<Model> getModel(int a) {

    dbHelper = new DbHelper(this);
    db = dbHelper.getWritableDatabase();
    Log.d("Model Int: ", "Model int: " +  a);
    sql = "SELECT * FROM shows";
    resultCursor = db.rawQuery(sql, new String [] {});
    resultCursor.moveToFirst();

    List<Model> list = new ArrayList<Model>();
        list.add(get(Html.fromHtml(resultCursor.getString(a)).toString()));

    for (resultCursor.moveToFirst(); resultCursor.moveToNext(); resultCursor
            .isAfterLast()) {

            list.add(get(Html.fromHtml(resultCursor.getString(a))
                    .toString()));

    }

    startManagingCursor(resultCursor);
    resultCursor.close();
    db.close();
    dbHelper.close();

    return list;
}

效果很好,但是当我不想使用 * 而是使用

sql = "SELECT title subtitle FROM Shows";

来获取所有列时。我收到这样的错误

错误/CursorWindow(371):字段槽 0,1 的请求错误。行数 = 162、列数 = 1

知道出了什么问题吗?

提前致谢。

I am pretty new to android, and I have the following problem.
This is my code where I'm trying to get data from the database into my list.

private List<Model> getModel(int a) {

    dbHelper = new DbHelper(this);
    db = dbHelper.getWritableDatabase();
    Log.d("Model Int: ", "Model int: " +  a);
    sql = "SELECT * FROM shows";
    resultCursor = db.rawQuery(sql, new String [] {});
    resultCursor.moveToFirst();

    List<Model> list = new ArrayList<Model>();
        list.add(get(Html.fromHtml(resultCursor.getString(a)).toString()));

    for (resultCursor.moveToFirst(); resultCursor.moveToNext(); resultCursor
            .isAfterLast()) {

            list.add(get(Html.fromHtml(resultCursor.getString(a))
                    .toString()));

    }

    startManagingCursor(resultCursor);
    resultCursor.close();
    db.close();
    dbHelper.close();

    return list;
}

that works fine, but when i do not want to get all the columns with * but with

sql = "SELECT title subtitle FROM shows";

instead. I get errors like this

ERROR/CursorWindow(371): Bad request for field slot 0,1. numRows =
162, numColumns = 1

Any ideas what's going wrong?

Thanks in advance.

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

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

发布评论

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

评论(2

赴月观长安 2024-12-15 05:33:19
SELECT title subtitle FROM shows

真正的意思

SELECT title AS subtitle FROM shows

是:仅选择一列:title,但在结果集中将该列重命名为“subtitle”。
这称为为列分配别名
请参阅:http://beginner-sql-tutorial.com/sql-aliases.htm

解决方案
执行:

SELECT title, subtitle FROM shows

现在您正在选择两列。

免责声明
我不保证该链接中信息的质量,网上有很多质量较差的教程。我刚刚查看了“别名”部分,部分看起来很有用。

SELECT title subtitle FROM shows

Really means

SELECT title AS subtitle FROM shows

In english: select only one column: title, but rename that column to "subtitle" in the resultset.
This is called assigning an alias to a column.
See: http://beginner-sql-tutorial.com/sql-aliases.htm

Solution
Do:

SELECT title, subtitle FROM shows

Now you are selecting two columns.

Disclaimer
I do not vouch for the quality of the info in that link, there are a lot of poor quality tutorials on the net. I just looked at the "alias" section and that section looks useful.

无人问我粥可暖 2024-12-15 05:33:19

我没有完全理解你的问题,但我认为你需要特定的字段,然后

select title, subtitle from shows 

定义用逗号分隔的字段

I didn't perfectly understand your question but I think u need specific field then

select title, subtitle from shows 

define the fields seperated by commas

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