获取最后一个 _id 时出现 CursorIndexOutOfBoundsException?

发布于 2024-11-17 06:11:19 字数 1685 浏览 5 评论 0原文

我正在尝试从数据库插入的最后一行(_id)中提取数据。我不知道我有什么错/遗漏。感谢您的帮助!

ERROR/AndroidRuntime(363): Caused by: android.database.CursorIndexOutOfBoundsException: 
                                      Index -1 requested, with a size of 1

爪哇:

 else { // Otherwise start the application here:

        Cursor c = db.rawQuery(
                "SELECT * FROM History_List ORDER BY _id desc limit 1",
                null);

        int nI = c.getColumnIndexOrThrow("intent");
        String intent = c.getString(nI);
        Class<?> nIntent = null;
        try {
            nIntent = Class.forName("com.aeroTechnologies.flyDroid."
                    + intent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int tvLabel = c.getColumnIndexOrThrow("label");
        String label = c.getString(tvLabel);

        int tvTitle = c.getColumnIndexOrThrow("title");
        String title = c.getString(tvTitle);

        int tvDescript = c.getColumnIndexOrThrow("description");
        String descript = c.getString(tvDescript);

        int tvURL = c.getColumnIndexOrThrow("gotoURL");
        String url = c.getString(tvURL);

        int yPOS = c.getColumnIndexOrThrow("scrollY");
        String ypos = c.getString(yPOS);

        Intent i = new Intent(Start.this, nIntent);

        i.putExtra("KEY_LABEL", label);
        i.putExtra("KEY_TITLE", title);
        i.putExtra("KEY_DESCRIPT", descript);
        i.putExtra("KEY_URL", url);
        i.putExtra("KEY_INTENT", intent);
        i.putExtra("KEY_YPOS", ypos);
        startActivity(i);
    }

I'm trying to pull data from the last row inserted (_id) of a database. I cant figure out what I have wrong / missing. Thnx for the help!!

ERROR/AndroidRuntime(363): Caused by: android.database.CursorIndexOutOfBoundsException: 
                                      Index -1 requested, with a size of 1

Java:

 else { // Otherwise start the application here:

        Cursor c = db.rawQuery(
                "SELECT * FROM History_List ORDER BY _id desc limit 1",
                null);

        int nI = c.getColumnIndexOrThrow("intent");
        String intent = c.getString(nI);
        Class<?> nIntent = null;
        try {
            nIntent = Class.forName("com.aeroTechnologies.flyDroid."
                    + intent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int tvLabel = c.getColumnIndexOrThrow("label");
        String label = c.getString(tvLabel);

        int tvTitle = c.getColumnIndexOrThrow("title");
        String title = c.getString(tvTitle);

        int tvDescript = c.getColumnIndexOrThrow("description");
        String descript = c.getString(tvDescript);

        int tvURL = c.getColumnIndexOrThrow("gotoURL");
        String url = c.getString(tvURL);

        int yPOS = c.getColumnIndexOrThrow("scrollY");
        String ypos = c.getString(yPOS);

        Intent i = new Intent(Start.this, nIntent);

        i.putExtra("KEY_LABEL", label);
        i.putExtra("KEY_TITLE", title);
        i.putExtra("KEY_DESCRIPT", descript);
        i.putExtra("KEY_URL", url);
        i.putExtra("KEY_INTENT", intent);
        i.putExtra("KEY_YPOS", ypos);
        startActivity(i);
    }

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

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

发布评论

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

评论(1

因为看清所以看轻 2024-11-24 06:11:19

将光标分配给 c 后,放置 c.moveToFirst(); 将其移动到行中的第一个元素。

所以,你将拥有

Cursor c = db.rawQuery(
            "SELECT * FROM History_List ORDER BY _id desc limit 1",
            null);
c.moveToFirst();  

After you assign the cursor to c, put a c.moveToFirst(); to move it to the first element in line.

So, you'll have

Cursor c = db.rawQuery(
            "SELECT * FROM History_List ORDER BY _id desc limit 1",
            null);
c.moveToFirst();  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文