我正在使用sqlite进行应用程序插入,更新数据库。虽然我插入和更新数据库。 app抛出了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]
答案 0 :(得分: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);
}
答案 1 :(得分:0)
查询中生成的Cursor可能没有任何行(结果为空,就像您的示例一样),ergo cursor.getCount()
将返回0
。
在这种情况下,您需要索引-1
上的元素,该元素始终无效。而不是手工制作,使用内置Cursor.moveToLast()
-method。您还可以检查是否有任何结果,因为此方法返回false
否则:
将光标移动到最后一行。
如果光标为空,将返回false 。
答案 2 :(得分:0)
仔细阅读错误消息显示,该查询未产生任何结果。
答案 3 :(得分:0)
问题在于:
int index=cursor.getInt(cursor.getColumnIndex(C_ID));
通过返回-1
cursor.getColumnIndex(C_ID)
检查是否已创建C_ID。