Reputation: 243
ı am making an application using sqlite which insert, update database. While ı'm insert and updating db. app throwed an CursorIndexOutOfBoundsException. ı know it is about cursor.Is there anyone who can help me?
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);
}
}
here is the exception:
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)
ok. ı did what you said.but after that when ı debug application it throws this.
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]
Upvotes: 0
Views: 3191
Reputation: 381
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);
}
Upvotes: 3
Reputation: 169
The problem is here :
int index=cursor.getInt(cursor.getColumnIndex(C_ID));
-1 is returned at by
cursor.getColumnIndex(C_ID)
check if C_ID has been created .
Upvotes: 0
Reputation: 43738
Carefully reading the error messages shows, that the query did not produce any results.
Upvotes: 0
Reputation: 25755
The resulting Cursor from your query might not have any rows in it (the result is empty, just like your example), ergo cursor.getCount()
will return 0
.
In this case, you want the element on index -1
, which is always invalid. Instead of hand-crafting this, use the build-in Cursor.moveToLast()
-method. You can also check if there where any results, since this method returns false
otherwise:
Move the cursor to the last row.
This method will return false if the cursor is empty.
Upvotes: 0