zoukankan      html  css  js  c++  java
  • android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1(zz)

    android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1 
    http://blog.csdn.net/competerh_programing/article/details/7396542

    android 中数据库处理,特别是使用cursor时,注意初始位置,好像是从下标为-1的地方开始的,也就是说一次查询中,返回给cursor查询结果时,不能够马上从cursor中提取值。

    比如,下面的代码会返回错误,android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0:

    int score =((Cursor)getReadableDatabase().query(TABLE_NAME,newString[]{"learned"},"_id=?",newString[]{""+id},null,null,null,"1")).getInt(0);


    正确的用法:
      Cursor cursor = getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1");
      int learned=0;
      if(cursor.moveToFirst()){
        score= cursor.getInt(0);
      }
      cursor.close();

     

  • 相关阅读:
    React Hooks 全解(一)
    Google搜索技巧
    #!/usr/bin/python3 和 #!/usr/bin/env python3 的区别
    Python函数
    Python程序代码阅读
    画个爱心向你表白
    自学需要注意的点
    Python文件操作
    国内加速访问GitHub
    (九) -前端-异步编程
  • 原文地址:https://www.cnblogs.com/bluestorm/p/3729526.html
Copyright © 2011-2022 走看看