0

我正在使用 sqlite 制作一个插入、更新数据库的应用程序。虽然我正在插入和更新数据库。应用程序抛出了 CursorIndexOutOfBoundsException。我知道这是关于光标的。有没有人可以帮助我?

public void EntryGiris(int yilsql, String aysql, int bakicisql,
        int krediArabasql, int krediOgrenimsql, int krediTatilsql,
        int faturaElektriksql, int faturaSusql, int faturaInternetsql,
        int aidatsql, int kaskoSigortasql) {

    ContentValues cv = new ContentValues();
    cv.put(C_YIL, yilsql);
    cv.put(C_AY, aysql);
    cv.put(C_BAKICI, bakicisql);
    cv.put(C_KREDIARABA, krediArabasql);
    cv.put(C_KREDIOGRENIM, krediOgrenimsql);
    cv.put(C_KREDITATIL, krediTatilsql);
    cv.put(C_FATURAELEKTRIK, faturaElektriksql);
    cv.put(C_FATURASU, faturaSusql);
    cv.put(C_FATURAINTERNET, faturaInternetsql);
    cv.put(C_AIDAT, aidatsql);
    cv.put(C_KASKOSIGORTA, kaskoSigortasql);

    String[] selectionArgs=new String[]{yilsql+"", aysql};
    String entryGirisSQL="SELECT c_id FROM harcamalar WHERE "+C_YIL+"= ? AND "+C_AY+"= ?";
    Cursor cursor=ourDatabase.rawQuery(entryGirisSQL, selectionArgs);
    cursor.moveToFirst();
    if(cursor.moveToLast()){
        int index=cursor.getInt(cursor.getColumnIndex(C_ID));
        if(index>=0)
            ourDatabase.update(DB_TABLE, cv, C_ID+"="+index, null);
        else ourDatabase.insert(DB_TABLE, null, cv);
    }

}

这是一个例外:

12-28 10:59:11.967: E/AndroidRuntime(317): FATAL EXCEPTION: main
12-28 10:59:11.967: E/AndroidRuntime(317): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
12-28 10:59:11.967: E/AndroidRuntime(317):  at com.deitel.btc.TemporaryDatabase.EntryGiris(TemporaryDatabase.java:129)
12-28 10:59:11.967: E/AndroidRuntime(317):  at com.deitel.btc.Butcegiris$1.onClick(Butcegiris.java:59)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.view.View.performClick(View.java:2408)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.view.View$PerformClick.run(View.java:8816)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.os.Handler.handleCallback(Handler.java:587)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.os.Looper.loop(Looper.java:123)
12-28 10:59:11.967: E/AndroidRuntime(317):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 10:59:11.967: E/AndroidRuntime(317):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 10:59:11.967: E/AndroidRuntime(317):  at java.lang.reflect.Method.invoke(Method.java:521)
12-28 10:59:11.967: E/AndroidRuntime(317):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 10:59:11.967: E/AndroidRuntime(317):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 10:59:11.967: E/AndroidRuntime(317):  at dalvik.system.NativeStart.main(Native Method)

好的。我做了你说的。但在那之后,当我调试应用程序时,它会抛出这个。

Thread [<1> main] (Suspended (exception CursorIndexOutOfBoundsException))   
    Butcegiris$1.onClick(View) line: 75 
    Button(View).performClick() line: 2408  
    View$PerformClick.run() line: 8816  
    ViewRoot(Handler).handleCallback(Message) line: 587 
    ViewRoot(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]  
4

4 回答 4

3
if(cursor.getCount() > 0) {
cursor.moveToPosition(cursor.getCount() - 1);
    Long index=cursor.getLong(cursor.getColumnIndex(C_ID));
    if(index>=0)
            ourDatabase.update(DB_TABLE, cv, C_ID+"="+index, null);
    else ourDatabase.insert(DB_TABLE, null, cv);

}
于 2012-12-28T11:38:29.963 回答
0

您查询的结果 Cursor 中可能没有任何行(结果为空,就像您的示例一样),ergocursor.getCount()将返回0.

在这种情况下,您需要 index 上的元素-1,它总是无效的。与其手工制作,不如使用内置Cursor.moveToLast()方法。您还可以检查是否有任何结果,因为此方法false否则返回:

将光标移动到最后一行。

如果光标为空,此方法将返回 false 。

于 2012-12-28T11:26:16.877 回答
0

仔细阅读错误消息显示,查询没有产生任何结果。

于 2012-12-28T11:26:46.270 回答
0

问题在这里:

int index=cursor.getInt(cursor.getColumnIndex(C_ID));

-1 返回

cursor.getColumnIndex(C_ID)

检查是否已创建 C_ID。

于 2012-12-28T11:27:30.900 回答