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. Windows计算器使用详解

    (1)Backspace:退格,删除当前输入数字中的最后一位 (2)CE:清除,清除显示的数字. (3)C:归零,清除当前的计算. (4)MC:清除存储器中的数值. (5)MR:将存于存储器中的数显示 ...

  2. SqlServer2008(R2) 数据库使用外网IP实例连接服务器

    1.打开sql2008,使用windows身份登录 2.登录后,右键选择"属性".左侧选择"安全性",选中右侧的"SQL Server 和 Windo ...

  3. ios消息的交互方式

    注意这些都是界面回传(即从第二个界面传到第一个界面,从第一个界面传到第二个界面的时候用第二个界面的属性即可)   iOS消息的交互方式有4种,分别为:通知,代理,block,kvo 现在我们对这个4中 ...

  4. jquery ajax提交及请求

    jQuery.ajax({ url: dataURL, success: function(results) { var parsedJson = jQuery.parseJSON(results); ...

  5. 超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上

    超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 阅读目录 Deft简介 Deft 核心类介绍 Deft 3分钟即可上手使用 其他可选的配置参数 性能测试 Demo代码下载 回到顶 ...

  6. HibernateDaoSupport的getSession()与HibernateTemplate的区别

    在 Spring+Hibernate的集成环境里,如果DAO直接使用HibernateDaoSupport的getSession()方法获取 session进行数据操作而没有显式地关闭该session ...

  7. perl /m修饰符使用说明

    高级用法: 多行匹配: grok正则和普通正则一样, 默认是不支持匹配回车换行的. perl的/m选项 The /m modifier allows ^ and $ to match immediat ...

  8. 在esx上 docker的网络桥接

    docker:/root# docker run -itd --net=none --name zjtest8_haproxy 192.168.32.150:5000/zjzc_centos6.5_m ...

  9. 字符串查找--B中是否有元素不在A中

    #include <stdio.h> int main(int argc, char const *argv[]) { char str[26]="AFDKJASD"; ...

  10. INPUT[type=file]的change事件不触发问题

    在网页上要操作文件通常会使用INPUT[type=file]控件,但这个控件的设计很蛋疼.它不像其它编程语言中文件选择后会触发一个事件,只是让上面的文字改变,而这个改变可能会触发change事件而已. ...