我正在使用 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]