如何清理 SQL 数据库?我这样做的方法失败了
我有一个具有以下方法的数据库:
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)
一如既往地感谢您的帮助。
I have a database with the following method:
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);
}}
The insert method is this:
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);
}
The testCount() method is this:
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;
}
The fetch method is this:
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;
}
When I run this I get an error:
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);
Here is my 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)
As always thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我找到了您的
异常
的根源。在活动
onClick()
方法中,您创建(并插入到数据库中)4 个注释,删除一个(注释 3),然后获取注释 3(这应该是已成为注释 3 的注释 4) )。您没有通过该方法发布的内容是Cursor c = mDbHelper.fetchNote(3);
行之后的其他行,您最有可能尝试使用该光标<代码>c。问题是您的
deleteNote()
方法按照您的设计工作:- 删除带有 id 的注释(在您的示例中为 3)
-
x=3
和y
变为 3(您已从插入的 4 行中删除了一行),因此当您进入while
循环时(x < y
) 条件为 false,因此您永远不会进入循环来将注释 4 更新为注释 3。现在您的数据库具有如下结构:
然后您尝试获取不存在的注释 3,最终得到一个空游标
c
,您尝试在其上工作并抛出CursorIndexOutOfBoundsException
。要解决此问题,请尝试更新您的逻辑,以免
KEY_REALROWID
列中出现空格。 (我还没有测试过,但尝试将while(x <= y)
放入deleteNote()
方法中)。嗯,这就是我认为正在发生的事情。
Ok i think i found the root of your
Exception
.In the activity
onClick()
method you create(and insert in the DB) 4 notes, delete one(note 3) and then you fetch the note 3(this should be note 4 that has become note 3). What you didn't post post from that method is other lines after the lineCursor c = mDbHelper.fetchNote(3);
where most likely you try to use that cursorc
.The problem is that your
deleteNote()
method works as you designed it:- deletes the note with the id (3 in you example)
-
x=3
andy
becomes 3(you have deleted one row from the 4 that you inserted) so when you get to thewhile
loop (x < y
) the condition is false so you never enter in to the loop to update the note 4 to become note 3.Now your database has a structure like this:
Then you try to fetch the note 3 that doesn't exist and you end up with an empty cursor
c
on which you try to work and throws theCursorIndexOutOfBoundsException
.To resolve the issue try to update your logic so you don't end up with empty spaces in the
KEY_REALROWID
column. (I haven't tested but try to putwhile(x <= y)
in thedeleteNote()
method).Well this is what i think it' happening.