我有一个使用以下方法的数据库:
public void deleteNote(long rowId) {
mDb.delete(DATABASE_TABLE, KEY_REALROWID + "=" + rowId, null);
int x = (int) rowId;
int y = testCount();
while(x<y)
{x++;
Cursor note= fetchNote(x);
ContentValues argsan = new ContentValues();
argsan.put(KEY_REALROWID, x-1);
long z = Integer.valueOf(note.getString
(note.getColumnIndexOrThrow(NotesDbAdapter.K EY_ROWID)));
mDb.update(DATABASE_TABLE, argsan, KEY_ROWID + "=" + z, null);
}}
插入方法是这样的:
public void createNote(double value, String isroot, String ispower,
String ismultiply, String isdivisor, String add,
String issubtract, double roototpowerval, String
paranthaseesend, String paranthaseesstart) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_VALUE, value);
initialValues.put(KEY_ISROOT, isroot);
initialValues.put(KEY_ISPOWER, ispower);
initialValues.put(KEY_ISMULTIPLIER, ismultiply);
initialValues.put(KEY_ISDIVISOR, isdivisor);
initialValues.put(KEY_ISADD, add);
initialValues.put(KEY_ISSUBTRACT, issubtract);
initialValues.put(KEY_POWERORROOTNUMBER, roototpowerval);
initialValues.put(KEY_REALROWID, testCount()+1);
initialValues.put(KEY_ISPE, paranthaseesend);
initialValues.put(KEY_ISPS, paranthaseesstart);
mDb.insert(DATABASE_TABLE, null, initialValues);
}
testCount() 方法是这样的:
public int testCount() {
Cursor c = mDb.rawQuery("select count(*) from notes", null);
int tst = 0;
if (c.moveToNext()) {
tst = c.getInt(c.getColumnIndex("count(*)"));
}
return tst;
}
获取方法是这样的:
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[]
{KEY_ISPS,KEY_ISPE,KEY_VALUE,
KEY_ISROOT,KEY_REALROWID,KEY_ISPOWER,KEY_POWERORROOTNUMBER,KEY_ISDIVISOR,
KEY_ISMULTIPLIER, KEY_ISADD,KEY_ISSUBTRACT},
KEY_REALROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
当我运行它时,我得到一个错误:
mDbHelper.createNote(d, "false", "false", "false", "false",
"false", "false", 0, "false", "false");
mDbHelper.createNote(0, "false", "false", "false","false",
"false", "true", 0, "false", "false");
mDbHelper.createNote(d, "false", "false", "false","false",
"false", "false", 0, "false", "false");
mDbHelper.createNote(d, "false", "false", "false","false",
"false", "false", 0, "false", "false");
mDbHelper.deleteNote(3);
Cursor c = mDbHelper.fetchNote(3);
这是我的 LogCat:
FATAL EXCEPTION: main
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
at com.real.AlgebraActivity$1.onClick(AlgebraActivity.java:301)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
一如既往地感谢您的帮助。