我正在尝试在此处进行查询,但我收到了错误消息。我不知道我的错误在哪里,请提供建议并帮助我解决这个问题。欢迎所有建议。提前谢谢。
我的SQL查询:
public User getSubTaskNumber () {
db = helper.getReadableDatabase();
User user = null;
Cursor cursor = null;
String[] columns = {SQLiteHelper.UID, String.valueOf((SQLiteHelper.TASK)), String.valueOf((SQLiteHelper.SUB_TASK))};
String selectQuery = " SELECT count(*) FROM " +SQLiteHelper.TABLE_USER + " WHERE " + SQLiteHelper.TASK + " =? " ;
cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()) {
{
user = new User((Integer.parseInt(cursor.getString(0))), (Integer.parseInt(cursor.getString(1))));
}
cursor.close();
db.close();
}
return user;
}
我在我的一项活动中设置了获取的值,如下所示:
User user = db.getSubTaskNumber();
subTaskNumber.setText(user.getSubTask());
我的用户表架构:
public static final String CREATE_TABLE_USER = " CREATE TABLE " + TABLE_USER + " ( " + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TASK + " INTEGER, " + SUB_TASK + " INTEGER, " + " FOREIGN KEY (" + TASK + ") REFERENCES " + TABLE_NAME + " (" + UID + " )" +
" FOREIGN KEY (" + SUB_TASK + ") REFERENCES " + TABLE_NAME_CHILD + " (" + UIDCHILD + ") );";
ADB追踪:
03-09 16:39:03.091 2080-2080/com.ms.t.tms E/CursorWindow﹕ Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
03-09 16:39:03.132 2080-2080/com.ms.t.tms D/AndroidRuntime﹕ Shutting down VM
03-09 16:39:03.142 2080-2080/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ms.t.tms, PID: 2080
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.DetailView}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.ms.t.tms.SQLiteDataBaseAdapter.getSubTaskNumber(SQLiteDataBaseAdapter.java:282)
at com.ms.t.tms.DetailView.onCreate(DetailView.java:88)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
更新的查询:
public User getSubTaskNumber () {
db = helper.getReadableDatabase();
User user = null;
Cursor cursor = null;
String[] columns = {SQLiteHelper.UID, String.valueOf((SQLiteHelper.TASK)), String.valueOf((SQLiteHelper.SUB_TASK))};
String selectQuery = " SELECT * FROM " +SQLiteHelper.TABLE_USER + " WHERE " + SQLiteHelper.TASK + " =? " ;
cursor = db.rawQuery(selectQuery, null);
if (cursor.getCount() > 0)
cursor.moveToFirst();
user = new User((Integer.parseInt(cursor.getString(0))), (Integer.parseInt(cursor.getString(1))));
cursor.close();
db.close();
return user;
}
答案 0 :(得分:2)
你应该改变
String selectQuery = " SELECT count(*) FROM " +SQLiteHelper.TABLE_USER + " WHERE " + SQLiteHelper.TASK + " =? " ;
到
String selectQuery = " SELECT * FROM " +SQLiteHelper.TABLE_USER + " WHERE " + SQLiteHelper.TASK + " =? " ;
cursor.getCount();
这将为您提供总计(affected row count
)。
然后你可以检查
if(cursor.getCount()>0){
cursor.moveToFirst();
}