从DB获取对象会返回空对象或应用程序崩溃

时间:2014-08-01 11:58:46

标签: android sqlite

在我的数据库管理器类中,我有一个方法可以使用group_db和trip_db参数检索selected_group。

public Selected_Group get_selected_group_by_group_id_and_trip_id(int trip_id, int group_id)
{
    open_readable();
    Cursor cursor = db.query(Constants.Table_Names.TABLE_NAME_SELECTED_GROUPS, null, "trip_id="+trip_id+"&group_id="+group_id, null, null, null, null);
    int sg_id = 0;
    int sg_trip_id = 0;
    int sg_group_id = 0;
    while(cursor.moveToNext())
    {
        sg_id = cursor.getInt(0);
        sg_trip_id = cursor.getInt(1);
        sg_group_id = cursor.getInt(2);
    }

    Selected_Group sg = new Selected_Group(sg_id, sg_trip_id, sg_group_id);
    cursor.close();
    close();
    return sg;
}

但是在这种情况下,游标会跳过整个while循环,并且我收到一个带有启动值的Selected_Group,即_id = 0.

所以我这样试了:

public Selected_Group get_selected_group_by_group_id_and_trip_id(int trip_id, int group_id)
{
    open_readable();
    Cursor cursor = db.query(Constants.Table_Names.TABLE_NAME_SELECTED_GROUPS, null, "trip_id="+trip_id+"&group_id="+group_id, null, null, null, null);

    cursor.moveToFirst();
    int sg_id = cursor.getInt(0);
    int sg_trip_id = cursor.getInt(1);
    int sg_group_id = cursor.getInt(2);
    Selected_Group sg = new Selected_Group(sg_id, sg_trip_id, sg_group_id);
    cursor.close();
    close();
    return sg;
}

在这种情况下,应用程序崩溃并显示以下错误消息:

08-01 14:31:17.416: E/AndroidRuntime(13385): FATAL EXCEPTION: main
08-01 14:31:17.416: E/AndroidRuntime(13385): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at com.mycompany.myapp.tasks.Task_DB_Manager.get_selected_group_by_group_id_and_trip_id(Task_DB_Manager.java:542)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at com.mycompany.myapp.activities.Activity_Add_Edit_Trip.onClick(Activity_Add_Edit_Trip.java:263)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.view.View.performClick(View.java:3511)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.view.View$PerformClick.run(View.java:14105)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.os.Looper.loop(Looper.java:137)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 14:31:17.416: E/AndroidRuntime(13385):    at dalvik.system.NativeStart.main(Native Method)

我检查了调试器,所有参数都正确传递。 SQLite浏览器显示我在相应的表中有这些参数的记录。

该表是在DB_Helper类中构建的:

// Create table selected groups
String create_table_selected_groups = "CREATE TABLE "+Constants.Table_Names.TABLE_NAME_SELECTED_GROUPS+" (_id INTEGER PRIMARY KEY AUTOINCREMENT"+
            ", trip_id INTEGER, group_id INTEGER);";
db.execSQL(create_table_selected_groups);

那为什么不归还?这段代码有什么问题?

1 个答案:

答案 0 :(得分:2)

&运算符适用于二进制数,其关联性高于=

你想要的是AND

"trip_id="+trip_id+" AND group_id="+group_id