zoukankan      html  css  js  c++  java
  • Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested

    android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的。

    也就是说返回给cursor查询结果时,不能够马上从cursor中提取值。

    下面的代码会返回错误
    User u = null;
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from user where id = ?", new String[] { id.toString() });
    Integer uid = cursor.getInt(cursor.getColumnIndex("id"));
    String uname = cursor.getString(cursor.getColumnIndex("name"));
    float uamount = cursor.getFloat(cursor.getColumnIndex("amount"));
    u = new User(uid, uname, uamount);

    正确的用法

    User u = null;
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from user where id = ?", new String[] { id.toString() });
    if (cursor.moveToFirst())
    {
     Integer uid = cursor.getInt(cursor.getColumnIndex("id"));
     String uname = cursor.getString(cursor.getColumnIndex("name"));
     float uamount = cursor.getFloat(cursor.getColumnIndex("amount"));
     u = new User(uid, uname, uamount);
    }

    http://blog.sina.com.cn/s/blog_67aaf44401015xtl.html

  • 相关阅读:
    CCF CSP 201403-2 窗口
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
  • 原文地址:https://www.cnblogs.com/chen110xi/p/3247317.html
Copyright © 2011-2022 走看看