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. (九)Android权限系统

    一.WebView请求权限实例 1.WebView获取网页访问权限的xml布局文件和MainActivity中的程序如下 <WebView android:layout_width=" ...

  2. WindowsForm 记事本 对话框

    textbox:     属性:         text:文本         selectedtext:获取或设置选中文本         canundo:是否能够撤销     方法:       ...

  3. 前端公共库cdn服务推荐//提高加载速度/节省流量

    前端公共库cdn服务推荐,使用可以提高js库加载速度同时也可以节省自己空间的流量,CDN加速公共库虽好,不过一定要使用靠谱的前端cdn服务提供方. 以下整理出比较靠谱的国内cdn加速服务器.排名不分先 ...

  4. global变量

    在函数体内定义的global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用, $global $a; $a=123; function f() { echo $a; //错 ...

  5. 一.ubuntu14.04安装、亮度设置、显卡设置等一体化讲解

    一.ubuntu14.04安装 安装步骤很简单的,相信你只要知道并且决定安装ubuntu,你就不会在安装上有问题,下载网址 http://www.ithome.com/html/soft/81539. ...

  6. html5 Canvas处理图像 实例讲解

    最近在学习canvas,canvas有很强大的图像处理功能,下面写一个我的学习总结: canvas常用功能: 1. 绘制矩形.圆形.曲线.组合图形 2. 绘制文本 3.绘制渐变.变形的图形 4. 图片 ...

  7. jquery ajax后台向前台传list 前台用jquery $.each遍历list

    $.ajax({ type: 'post', url: xxx.action', dataType: 'text', success: function(data){ var dataObj=eval ...

  8. Django模型-数据库操作

    前言 前边记录的URLconf和Django模板全都是介绍页面展示的东西,也就是表现层的内容.由于Python先天具备简单而强大的数据库查询执行方法,Django 非常适合开发数据库驱动网站. 这篇开 ...

  9. WebMatrix安装和使用

    官网:http://www.microsoft.com/web/webmatrix/ 一直觉得dreamweaver已经过时了,很多新的库都不支持.而且,启动慢,占用内存多,是时候换一个ide了. h ...

  10. CC++初学者编程教程(12) 基于rhel6.3的Oracle数据库学习环境搭建

    前言 安装oracle 11g系统最好是1G以上内存,硬盘至少需要4.5G空间. 至少环境在Linux Server release 5.3以上. win安装包 win32_11gR2_databas ...