java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. 
Make sure the Cursor is initialized correctly before accessing data from it. 
 
SQLiteDatabase  学习过程中,使用query() 查询表单数据时,遇到Couldn't read row 0, col -1 from CursorWindow错误。
排除建表字段错误、拼写错误、query()输入字段错误。当我使用rawQuery()方法取代query()方法,问题就解决了。
 
代码如下:
1.建表 MyDatabaseHelper

     private static final String CREATE_CONTACTS = "create table contact ("
+ "id integer primary key autoincrement, "
+ "name text, "
+ "number text )"; public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CONTACTS);
}
2.查询 根据searchName查询表单
     private void searchContact(String searchName) {
db = dbHelper.getReadableDatabase();
//Cursor cursor = db.query("contact", new String[] {"name"}, "name = ?", new String[] {searchName}, null, null, null);
Cursor cursor = db.rawQuery("select * from contact where name = ?", new String[] {searchName});
if(cursor.moveToFirst()) {
//String name = cursor.getString(cursor.getColumnIndex("name"));
String number = cursor.getString(cursor.getColumnIndex("number"));
tv_name.setText(searchName);
tv_number.setText(number);
tel = "tel:" + number;
}else {
tv_name.setText("404! Not Found");
tv_number.setText("10086");
tel = "tel:10086";
}
cursor.close();
db.close();
}

1.当使用query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

String name = cursor.getString(cursor.getColumnIndex("name"))

用这句代码检验 可以获得到name,证明cursor正确指向所要查询数据。

String number = cursor.getString(cursor.getColumnIndex("number"))

但这句代码就会报Couldn't read row 0, col -1 from CursorWindow。

getColumnIndex("number") 返回值竟然是-1,百思不得其解!

2.使用rawQuery(String sql, String[] selectionArgs)

name和number字段都可以准确获得,问题完美解决。

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4128299.html

Couldn't read row 0, col -1 from CursorWindow的更多相关文章

  1. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  2. Xamarin.Android 使用 SQLite 出现 Couldn't read row 0, col -1 from CursorWindow. 异常

    异常:Java.Lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cu ...

  3. Android Exception 8(Couldn't read row 0, col -1 from CursorWindow)

    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Curso ...

  4. 【我的Android进阶之旅】解决sqlcipher库:java.lang.IllegalStateException: get field slot from row 0 col 0 failed.

    一.背景 最近维护公司的大数据SDK,在大数据SDK里面加入了ANR的监控功能,并将ANR的相关信息通过大数据埋点的方式记录到了数据库中,然后大数据上报的时候上报到大数据平台,这样就可以实现ANR性能 ...

  5. java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data fr

    Android中操作Sqlite遇到的错误:java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. ...

  6. Jquery DataTables warning : Requested unknown from the data source for row 0

    昨天在做 Jquery DataTables 的时候,遇到的一个问题,我使用MVC,在tables上加入了一个actionlink的href.但是在运行起来的时候,报错: DataTables war ...

  7. DataTables warning : Requested unknown parameter '0' from the data source for row 0错误

    在做datatables的项目,从后台取得数据后,返回给datatables界面时会报下面的错误: DataTables warning : Requested unknown parameter ' ...

  8. DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For more

    重点内容 DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For ...

  9. Bootstrap《第一篇》,关于container、jumbotron、row、col、text-center等的学习

    一.关于引入bootstrap文件 <!-- 为了确保适当的绘制和触屏缩放,需要在 <head> 之中添加 viewport 元数据标签. --> <meta name= ...

随机推荐

  1. 反编译C#的dll文件并修改,再重新生成dll

    1.把dll文件导入到ildasm工具中,ildasm是由微软提供的.net程序反编译工具,位于“C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A ...

  2. java集合类遍历删除方法测试以及使用场景记录

    package test0; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java. ...

  3. mysql 更新 语句中 的 safe_mode

    在mysql5中,可以设置safe mode,比如在一个更新语句中UPDATE table_name SET bDeleted=0;执行时会错误,报:You are using safe update ...

  4. html5 canvas 画hello ketty

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  5. JavaScript学习笔记(三)this关键字

    this是Javascript的关键字,代表在函数运行时,自动生成一个内部对象,只能在函数内部使用.例如: function test() { this.x = 1; } 随着函数的使用场合不同,th ...

  6. yum 安装软件时报Public key for * is not installed

    这个是由于没有导入rpm签名信息引起的 解决方案: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

  7. struts2之动态方法调用(转)

    转自:http://blog.csdn.net/longwentao/article/details/6940289 当我们访问一个Action时,默认是访问execute()方法,但当在一个Acti ...

  8. php 汉字转换成拼音

    class PinYin { private static $__data = array(); private static $__init = false; private static func ...

  9. axis2 webservices 411错误解决办法

    错误:org.apache.axis2.AxisFault: Transport error: 411 Error: Length Required 可能会导致这个问题的原因: 1.访问地址经过端口映 ...

  10. 鼠标拖放div 实现

    Javascript的mousemove事件类型是一个实时响应的事件,当鼠标指针的位置发生变化时(至少移动1个像素),就会触发mousemove事件.该事件响应的灵敏度主要参考鼠标指针移动速度的快慢, ...