Android从数据库错误中选择一行

时间:2013-04-25 16:09:31

标签: android

我是Android新手并尝试从数据库中选择一行,如下所示:

public Cursor getEntry(int id){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor entry = db.query(TABLE_NAME, new String[]{
            "id", "title", "description", "image_path"
    }, "id" + "=?", new String[]{ String.valueOf(id) }, null, null, null, null);

    return entry;
}

并试图像这样输出:

    DatabaseHelper db = new DatabaseHelper(this);
    TextView tv = new TextView(this);
    if (db.getEntry(1) != null){
        while (db.getEntry(1).moveToFirst()){
            tv.setText("" + db.getEntry(1).getString(1));
        }
    }
    setContentView(tv);

和错误:

ERROR/AndroidRuntime(1850): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.untitled1/com.example.untitled1.DisplayMessageActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at com.example.untitled1.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:28)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    ... 11 more

有什么问题?

由于

3 个答案:

答案 0 :(得分:2)

while (db.getEntry(1).moveToFirst()){
    tv.setText("" + db.getEntry(1).getString(1));

每次执行db.getEntry(1)时,您都要执行两次查询。所以第二次,你在另一个没有用getString(1)正确设置的Cursor上调用moveToFirst()

只需调用一次,并将生成的Cursor存储在局部变量中。此外,不要忘记在完成后安全地关闭光标。

Cursor c = db.getEntry(1);
try {
    if (c.moveToFirst()) 
        tv.setText(c.getString(1));
} finally {
    c.close();
}

答案 1 :(得分:1)

试试这个

db.getEntry(1)调用两次将返回另一个Cursor,因此当您在第一个Cursor上调用moveToFirst()并访问第二个

时,它不会移动到firstRow
Cursor cursor=db.getEntry(1);

if(cursor.moveToFirst())
{
 tv.setText("" + cursor.getString(1));
}

注意:查询方法永远不会将Cursor返回为Null。所以不需要检查Null。

答案 2 :(得分:1)

您只需要执行一次查询,为什么要多次调用getEntry()?并且while(cursor.moveToFirst())毫无意义,如果Cursor有结果,则它是无限循环。

Cursor cursor = getEntry(1);
try {
    if (cursor.moveToNext()){
       tv.setText(cursor.getString(1));
    }
} finally {
    cursor.close(); // Close the Cursor when you're finished with it, or use a Loader.
}