获取Last _id时出现CursorIndexOutOfBoundsException?

时间:2011-06-25 02:10:59

标签: android database cursor

我正在尝试从数据库的最后一行插入(_id)中提取数据。我无法弄清楚我错了/遗失了什么。 Thnx的帮助!!

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

1 个答案:

答案 0 :(得分:7)

将光标指定给c后,放置一个c.moveToFirst();将其移动到第一个元素。

所以,你有

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