简体   繁体   English

记录不会从SQLite数据库中删除

[英]Record Won't Delete from SQLite Database

I'm currently working on my A Level Computer Science Controlled Assessment, making a Scout Manager application in Android Studio, Java and XML, none of which I have used before. 我目前正在进行A Level计算机科学控制评估,在Android Studio,Java和XML中制作一个Scout Manager应用程序,我以前都没用过。 I'm now on the last stretch, but I've run into an error which I can't seem to solve. 我现在正处于最后阶段,但我遇到了一个我似乎无法解决的错误。

I'm trying to delete a session record from the events table. 我正在尝试从事件表中删除会话记录。 This has worked perfectly fine before, however for some reason, despite no change in code (that I can remember, anyway), it no longer does. 这在以前工作得很好,但是出于某种原因,尽管代码没有变化(我记得,无论如何),它不再是。

Here is the code from the "Edit Event" activity: 以下是“编辑事件”活动中的代码:

//Deletes the event from the database
    public void deleteEvent(View view)
    {
        boolean result=DatabaseHandler.deleteSession(sessionid);
        if (result==true)
        {
            Toast.makeText(
                    context,
                    "Event Deleted",
                    Toast.LENGTH_SHORT
            ).show();
            backToProgramme(view);
        }
        else
        {
            Toast.makeText(
                    context,
                    "Error - please try again",
                    Toast.LENGTH_SHORT
            ).show();
        }
    }

And here is the code from the databaseHandler: 这是来自databaseHandler的代码:

//Deleting a session in the events table
    public boolean deleteSession(int id)
    {
        boolean result = false;
        String query = "SELECT * FROM " + TABLE_EVENTS + " WHERE " + COLUMN_SEID + " = '"
                + String.valueOf(id) + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        sessionDetails session = new sessionDetails();
        if (cursor.moveToFirst())
        {
            session.setSessionID(Integer.parseInt(cursor.getString(0)));
            db.delete (TABLE_EVENTS, COLUMN_SCID + "=?", new String[]{String.valueOf(
                    session.getSessionID())});
            cursor.close();
            result = true;
        }
        return result;
    }

I can't at all work out why this isn't deleting the record. 我无法解释为什么这不是删除记录。

What you are doing here is: 你在这做的是:
search the table TABLE_EVENTS to find a row with COLUMN_SEID = id and if found then get the value of column COLUMN_SCID and delete the row where COLUMN_SCID equals that value! 搜索表TABLE_EVENTS以查找具有COLUMN_SEID = id的行,如果找到,则获取列COLUMN_SCID的值并删除COLUMN_SCID等于该值的行!
Why all this? 为什么这一切?
Can't you just delete the row with COLUMN_SEID = id ? 你不能只删除COLUMN_SEID = id的行吗?
I believe that your code should be written like this: 我相信你的代码应该是这样编写的:

public boolean deleteSession(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_EVENTS, COLUMN_SEID + " = ?", new String[]{String.valueOf(id)}) > 0;
}

The method delete() returns the number of deleted rows, so deleteSession() will return false if no row was deleted. delete()方法返回已删除行的数量,因此如果没有删除行, deleteSession()将返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM