java.lang.IllegalStateException: 无法从 CursorWindow 读取第 0 行,列 -1
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow
伙计们。
我的应用程序中有一个数据库。
数据库是以编程方式创建的。
我想从数据库中获取数据,return列索引是 -1。
数据库中有数据,但列索引为-1,我无法return数据。
如何解决这个问题?
这是我的代码;
向数据库添加项目的代码。
String task = "cook lunch";
helper = new TaskDBHelperHome(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
values.put("task", task);
db.insertWithOnConflict(TaskContractHome.TABLE, null, values,
SQLiteDatabase.CONFLICT_IGNORE);
以及显示数据的代码。
helper = new TaskDBHelperHome(mcontext);
SQLiteDatabase homeDB = helper.getReadableDatabase();
Cursor cursor = homeDB.rawQuery("SELECT COUNT(*) FROM hometasks", null);
cursor.moveToFirst();
if (cursor != null && cursor.moveToFirst()) {
if (cursor.getInt(0) == 1) {
cursor.moveToFirst();
Integer indexnum = cursor.getColumnIndex("task");
String index = Integer.toString(cursor.getColumnIndex("task"));
String hometodo = cursor.getString(indexnum);
Log.d("DatabaseTodoListManager", "Your to-do is " + hometodo);
//expected to return the data "cook lunch",
//but shows the error java.lang.IllegalStateException
cursor.close();
}
}
TaskDBHelperHome.java
public class TaskDBHelperHome extends SQLiteOpenHelper {
public TaskDBHelperHome(Context context) {
super(context, TaskContractHome.DB_NAME, null, TaskContractHome.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"%s TEXT)", TaskContractHome.TABLE,
TaskContractHome.Columns.TASK);
Log.d("TaskDBHelper","Query to form table: "+sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContractHome.TABLE);
onCreate(sqlDB);
}
}
TaskContractHome.java
public class TaskContractHome {
public static final String DB_NAME = "home";
public static final int DB_VERSION = 1;
public static final String TABLE = "hometasks";
public class Columns {
public static final String TASK = "task";
public static final String _ID = BaseColumns._ID;
}
}
Logcat 附加信息错误:
E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.bedrock.live, PID: 14081
E/AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.bedrock.live.service.WiFiService: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2601)
E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime: at android.database.CursorWindow.nativeGetString(Native Method)
E/AndroidRuntime: at android.database.CursorWindow.getString(CursorWindow.java:438)
E/AndroidRuntime: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.homeNotification(WiFiService.java:106)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.count(WiFiService.java:82)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.Wifi(WiFiService.java:64)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.onReceive(WiFiService.java:45)
E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2594)
E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
你这样定义光标:
cursor = homeDB.rawQuery("SELECT COUNT(*) FROM hometasks", null);
聚合函数COUNT(*)
的结果没有列名。所以,
indexnum = cursor.getColumnIndex("task");
将“-1”分配给 'indexnum'。
查询只会给你一个整数值(行数),如果你想要"task"列的值,你需要一个不同的查询。
剧透:
Cursor cursor = homeDB.rawQuery("SELECT tasks FROM hometasks", null);
伙计们。
我的应用程序中有一个数据库。
数据库是以编程方式创建的。
我想从数据库中获取数据,return列索引是 -1。
数据库中有数据,但列索引为-1,我无法return数据。
如何解决这个问题?
这是我的代码;
向数据库添加项目的代码。
String task = "cook lunch";
helper = new TaskDBHelperHome(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
values.put("task", task);
db.insertWithOnConflict(TaskContractHome.TABLE, null, values,
SQLiteDatabase.CONFLICT_IGNORE);
以及显示数据的代码。
helper = new TaskDBHelperHome(mcontext);
SQLiteDatabase homeDB = helper.getReadableDatabase();
Cursor cursor = homeDB.rawQuery("SELECT COUNT(*) FROM hometasks", null);
cursor.moveToFirst();
if (cursor != null && cursor.moveToFirst()) {
if (cursor.getInt(0) == 1) {
cursor.moveToFirst();
Integer indexnum = cursor.getColumnIndex("task");
String index = Integer.toString(cursor.getColumnIndex("task"));
String hometodo = cursor.getString(indexnum);
Log.d("DatabaseTodoListManager", "Your to-do is " + hometodo);
//expected to return the data "cook lunch",
//but shows the error java.lang.IllegalStateException
cursor.close();
}
}
TaskDBHelperHome.java
public class TaskDBHelperHome extends SQLiteOpenHelper {
public TaskDBHelperHome(Context context) {
super(context, TaskContractHome.DB_NAME, null, TaskContractHome.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"%s TEXT)", TaskContractHome.TABLE,
TaskContractHome.Columns.TASK);
Log.d("TaskDBHelper","Query to form table: "+sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContractHome.TABLE);
onCreate(sqlDB);
}
}
TaskContractHome.java
public class TaskContractHome {
public static final String DB_NAME = "home";
public static final int DB_VERSION = 1;
public static final String TABLE = "hometasks";
public class Columns {
public static final String TASK = "task";
public static final String _ID = BaseColumns._ID;
}
}
Logcat 附加信息错误:
E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.bedrock.live, PID: 14081
E/AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.bedrock.live.service.WiFiService: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2601)
E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime: at android.database.CursorWindow.nativeGetString(Native Method)
E/AndroidRuntime: at android.database.CursorWindow.getString(CursorWindow.java:438)
E/AndroidRuntime: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.homeNotification(WiFiService.java:106)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.count(WiFiService.java:82)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.Wifi(WiFiService.java:64)
E/AndroidRuntime: at com.bedrock.live.service.WiFiService.onReceive(WiFiService.java:45)
E/AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:2594)
E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:144)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5238)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
你这样定义光标:
cursor = homeDB.rawQuery("SELECT COUNT(*) FROM hometasks", null);
聚合函数COUNT(*)
的结果没有列名。所以,
indexnum = cursor.getColumnIndex("task");
将“-1”分配给 'indexnum'。
查询只会给你一个整数值(行数),如果你想要"task"列的值,你需要一个不同的查询。
剧透:
Cursor cursor = homeDB.rawQuery("SELECT tasks FROM hometasks", null);