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. JavaScript Dispatch Event

    <html> <head> <script type="text/javascript"> function performClick(elem ...

  2. yum mysql

    linux下使用yum安装mysql   1.安装 查看有没有安装过:           yum list installed mysql*           rpm -qa | grep mys ...

  3. tcp窗口滑动以及拥塞控制

    转自:http://blog.chinaunix.net/uid-26275986-id-4109679.html TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥 ...

  4. 浏览器文档播放Shockwave Flash 插件问题

    浏览器被提示shockwave flash crashed怎么办?在使用浏览器的时候经常被提示shockwave flash crashed,flash插件崩溃,网页就会出现一些无法显示的文件,下面绿 ...

  5. Session和Cookie的关系

    Session和Cookie关系 两者构建了web的回话数据 Cookie作为客户端的回话,Session为服务器端的 共同点: 都是1对1的,(一个客户一个独立的回话) 都以键值对的方式存储数据 都 ...

  6. Ghost win7 系统安装(虚拟机)

    1.将Ghost.iso添加到数据存储iso文件中,启动虚拟机,根据相关提示,文件格式化系统硬盘,完成操作后关机: 2.进入BIOS,设置从CDROM启动系统(否则系统会提示找到引导文件): 3.将系 ...

  7. HttpSQS

    http://goaheadtw.iteye.com/blog/1669682 http://zyan.cc/httpsqs/7/1/

  8. 用gdb调试程序笔记: 以段错误(Segmental fault)为例

    用gdb调试程序笔记: 以段错误(Segmental fault)为例[转] 1.背景介绍2.程序中常见的bug分类3.程序调试器(如gdb)有什么用4.段错误(Segmental fault)介绍5 ...

  9. 如何将你牛逼的iOS代码分享到CocoaPod(转)

    为了让读者一目了然,现在就从新建工程到最后提交podspec,简单粗暴的讲一通.Cocoapods不用解释太多,一句话...它是目前iOS开发中最广为使用的第三方库依赖管理工具. 下面开始讲创建pod ...

  10. protobuf使用错误总结

    1>HelloWorldScene.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall LoginReqMessage ...