FC在SQLite中查找记录时

时间:2012-06-02 21:16:50

标签: android sqlite

我有几种方法可以在我的应用中获取记录 但由于某种原因,这个人一直给我FC! 我正在使用以下方法来获取Cursor:

Cursor c=helper.getRecordById(num);

实现如下:

public Cursor getRecordById(int rowId) {
    String rowIDs=""+rowId;
    String SelectById = "SELECT id, OrderName, OrderLink, DateYear, DateMonth, DateDay, OrderPrice FROM Orders WHERE id=?";
    String[] args={rowIDs};
    return(getReadableDatabase().rawQuery(SelectById, args));
}

我也试过没有将Int转换为String但我只是这样离开..两种方式都得到同样的错误。

每当我到达以下行:

String name=c.getString(c.getColumnIndexOrThrow("OrderName"));

我得到了FC。 GetColumnIndex工作得很好......我已经完全测试了它。 但是getString会抛出错误。 :

06-02 21:07:13.656: W/KeyCharacterMap(4329): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-02 21:07:16.006: D/AndroidRuntime(4329): Shutting down VM
06-02 21:07:16.006: W/dalvikvm(4329): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-02 21:07:16.026: E/AndroidRuntime(4329): FATAL EXCEPTION: main
06-02 21:07:16.026: E/AndroidRuntime(4329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Sagi.MyOrders/com.Sagi.MyOrders.DisplayRecord}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.os.Looper.loop(Looper.java:123)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at java.lang.reflect.Method.invokeNative(Native Method)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at java.lang.reflect.Method.invoke(Method.java:521)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at dalvik.system.NativeStart.main(Native Method)
06-02 21:07:16.026: E/AndroidRuntime(4329): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at com.Sagi.MyOrders.DisplayRecord.onCreate(DisplayRecord.java:48)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 21:07:16.026: E/AndroidRuntime(4329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 21:07:16.026: E/AndroidRuntime(4329):     ... 11 more
06-02 21:12:16.126: I/Process(4329): Sending signal. PID: 4329 SIG: 9

有趣的是我复制了我在其他地方使用的相同的Lookup方法,它工作得很好!我甚至尝试给它一个常量来查找我知道存在只是为了检查。但没什么。错误一遍又一遍!

谢谢!!!

1 个答案:

答案 0 :(得分:1)

Cursor最初指向开始前的记录,以允许这种代码:

final Cursor cursor = /* get cursor */;
while(cursor.moveToNext()) {
    /* do something with cursor */
}

如果您只想要一条记录,并且您确定会有一条记录,那么在访问Cursor之前,请执行以下操作:

cursor.moveToNext();

或:

cursor.moveToFirst();