Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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

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 使用Sqlite DB时获取CursorIndexOutOfBoundsException_Android_Sqlite - Fatal编程技术网

Android 使用Sqlite DB时获取CursorIndexOutOfBoundsException

Android 使用Sqlite DB时获取CursorIndexOutOfBoundsException,android,sqlite,Android,Sqlite,我使用这段代码从游标获取计数。在SQLite管理器中执行Select查询时,我得到的计数为6 public int getCount(String name){ Cursor cursor; cursor = myDataBase.rawQuery( "select count(*) as _count from tProviderNode where parentId IN(select identifier

我使用这段代码从游标获取计数。在SQLite管理器中执行Select查询时,我得到的计数为6

public int getCount(String name){           
        Cursor cursor;
        cursor = myDataBase.rawQuery(
                "select count(*) as _count from tProviderNode where parentId IN(select identifier from tProviderNode where name='"
                        + name + "');", null);
        return cursor.getInt(cursor.getColumnIndex("_count"));
    }  
在执行此函数时,我得到以下错误:-

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

为什么我会犯这个错误?我遗漏了什么。

在使用
光标之前调用
光标。moveToFirst()

// ...
cursor.moveToFirst();
return cursor.getInt(cursor.getColumnIndex("_count"));

在使用
光标之前调用
光标。moveToFirst()

// ...
cursor.moveToFirst();
return cursor.getInt(cursor.getColumnIndex("_count"));

调用cursor.moveToFirst()来解决问题。

调用cursor.moveToFirst()来解决问题。

可能是您的姓名与db列的值不匹配,您的db col可能包含空格。在提问之前进行一点搜索可以立即为您提供答案。可能您的姓名与db列的值不匹配,也可能您的db col包含空格。在提问之前进行一点搜索可以立即为您提供答案