开发者社区> 问答> 正文

android在数据库中查找相应的数据时出错。

Cursor cursor=myDatabase.rawQuery("select ids,title,times from goodtime" , null);
 cursor.moveToFirst();
 while(!cursor.isAfterLast()){
 int id=cursor.getInt(cursor.getColumnIndex("ids"));
 String title=cursor.getString(cursor.getColumnIndex("title"));
 String times=cursor.getString(cursor.getColumnIndex("times"));
 System.out.println(title);
 System.out.println(cursor.getColumnIndex("bj"));
 String beijing=cursor.getString(cursor.getColumnIndex("bj"));
上面是我的代码,title还是可以正常查找的,但是bj这个变量的值确实-1,同时报出错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notebook/com.example.notebook.Notebook}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
这是我创建的表:
    String ml="create table goodtime(ids integer PRIMARY KEY autoincrement,title text,content text,times text,ptpath text,mppath text,vdpath text,islock integer,password text,bj text)";
    arg0.execSQL(ml);
网上说有可能大小写不对,我仔细查看了变量名都是一致的啊

展开
收起
爵霸 2016-06-12 13:38:28 2154 0
1 条回答
写回答
取消 提交回答
  • 查询结果为空的话,会出现这个错误。先判断cursor中是否有数据。cursor.getCount()

    2019-07-17 19:33:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载