Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即使在使用moveToFirst()并检查光标值时,cursorIndexOutOfBounds也会出错_Android_Sqlite - Fatal编程技术网

Android 即使在使用moveToFirst()并检查光标值时,cursorIndexOutOfBounds也会出错

Android 即使在使用moveToFirst()并检查光标值时,cursorIndexOutOfBounds也会出错,android,sqlite,Android,Sqlite,我已经努力解决这个可怜的问题好几个小时了。我在我的应用程序中使用了很多其他的光标方法,这些方法很有魅力,但这一个确实很难解决 public int getCategoryCode(String category){ int d=0; Cursor cg=database.query("categoryTable", null, "category_name='"+category+"'", null, null, null, null); cg.moveToFir

我已经努力解决这个可怜的问题好几个小时了。我在我的应用程序中使用了很多其他的光标方法,这些方法很有魅力,但这一个确实很难解决

public int getCategoryCode(String category){
    int d=0;
    Cursor cg=database.query("categoryTable", null, "category_name='"+category+"'", null, null, null, null);
        cg.moveToFirst();   
    Log.d("success", String.valueOf(cg.getColumnIndex("_id")));

    Log.d("success", String.valueOf(cg.getCount()));

    cg.getInt(cg.getColumnIndex("_id"));
    return d;
当我试图检索保存在列“\u id”中的“整数”时,程序返回异常。我检查了“id”列中是否存在结果,因为cg.getColumnIndex()在logcat中返回0。请帮忙

尝试检查:

if((cg!= null) && (cg.getCount() > 0)){
  cg.moveToFirst();
  //DO YOUR STUFF
}
试试这个(已编辑)!!:


使用以下代码

cursor.moveToFirst();
if(cursor.isAfterLast() == false) {

    // Your logic goes here
}

此外,您还可以确保表中有数据,使用断点确保查询正确,在查询完成后查看游标对象是否正确。

您不会像那样调用
moveToFirst()
。如果(moveToFirst()),则执行
cursor.moveToFirst();
if(cursor.isAfterLast() == false) {

    // Your logic goes here
}