如何清理 SQL 数据库?我这样做的方法失败了

发布于 2025-01-07 20:14:09 字数 3841 浏览 4 评论 0原文

我有一个具有以下方法的数据库:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-01-14 20:14:09

好吧,我想我找到了您的异常的根源。
在活动 onClick() 方法中,您创建(并插入到数据库中)4 个注释,删除一个(注释 3),然后获取注释 3(这应该是已成为注释 3 的注释 4) )。您没有通过该方法发布的内容是 Cursor c = mDbHelper.fetchNote(3);之后的其他行,您最有可能尝试使用该光标<代码>c。

问题是您的 deleteNote() 方法按照您的设计工作:
- 删除带有 id 的注释(在您的示例中为 3)
- x=3y 变为 3(您已从插入的 4 行中删除了一行),因此当您进入 while 循环时(x < y) 条件为 false,因此您永远不会进入循环来将注释 4 更新为注释 3。
现在您的数据库具有如下结构:

note 1
note 2
note 4

然后您尝试获取不存在的注释 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 line Cursor c = mDbHelper.fetchNote(3); where most likely you try to use that cursor c.

The problem is that your deleteNote() method works as you designed it:
- deletes the note with the id (3 in you example)
- x=3 and y becomes 3(you have deleted one row from the 4 that you inserted) so when you get to the while 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:

note 1
note 2
note 4

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 the CursorIndexOutOfBoundsException.

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 put while(x <= y) in the deleteNote() method).
Well this is what i think it' happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文