Android 删除带有多个子句的where子句的SQLite行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12162549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:56:24  来源:igfitidea点击:

Delete SQLite Row with where clause with multiple clauses

androidsqlitewhere-clausesql-delete

提问by Gareth Bowen

I've been searching around and not managed to find a solution or working example so here goes.

我一直在四处寻找,但没有找到解决方案或工作示例,所以这里是。

I've set up an SQLite database which has five columns with different fields which effectively builds a list for the user. There will be multiple values in the list that are the same, save for the ROWID which is set to auto increment.

我已经建立了一个 SQLite 数据库,它有五列不同的字段,可以有效地为用户构建一个列表。列表中会有多个相同的值,除了设置为自动递增的 ROWID。

Neither the user nor the program will know the ROWID once a value has been entered into the database, so I want for the user to be able to remove one row if it contains all four of the other fields.

一旦将值输入到数据库中,用户和程序都不会知道 ROWID,因此我希望用户能够删除包含所有四个其他字段的一行。

My code looks like this:

我的代码如下所示:

Database
            .delete(DATABASE_TABLE,
                    "KEY_DATE='date' AND KEY_GRADE='style2' AND KEY_STYLE='style' AND KEY_PUMPLEVEL='pumpLevel'",
                    null);

But this does not remove any values. I'm almost certain that the syntax of this command is to blame.

但这不会删除任何值。我几乎可以肯定这个命令的语法是罪魁祸首。

How do I format a where clause for delete command with multiple where clauses?

如何使用多个 where 子句格式化 delete 命令的 where 子句?

Solution from below:

从下面的解决方案:

ourDatabase.delete(DATABASE_TABLE, "ROWID = (SELECT Max(ROWID) FROM "
            + DATABASE_TABLE + " WHERE " + "date=? AND grade=? AND "
            + "style=? AND pump_level=?)", new String[] { date, grade,
            style, pumpLevel });

回答by Sam

I have a feeling that KEY_DATE, KEY_GRADE, etc are you Java variable names notyour actual column names. Try changing your code to this:

我有一种感觉KEY_DATEKEY_GRADE, 等是您的 Java 变量名,而不是您的实际列名。尝试将您的代码更改为:

.delete(DATABASE_TABLE,
        KEY_DATE + "='date' AND " + KEY_GRADE + "='style2' AND " +
        KEY_STYLE + "='style' AND " + KEY_PUMPLEVEL + "='pumpLevel'",
        null);

Also I assume that 'date', 'style', etc are stored in local variables, you should use the whereArgsparameter to project yourself from SQL Injection Attacks:

此外,我认为'date''style'等存储在本地变量,你应该使用的whereArgs参数从Project自己的SQL注入攻击

.delete(DATABASE_TABLE,
        KEY_DATE + "=? AND " + KEY_GRADE + "=? AND " +
        KEY_STYLE + "=? AND " + KEY_PUMPLEVEL + "=?",
        new String[] {date, grade, style, pumpLevel});

(where date = "date", grade = "style2", etc)

(其中date = "date"grade = "style2"等)



Added from comments

从评论中添加

To delete the last row use this:

要删除最后一行,请使用:

.delete(DATABASE_TABLE,
        "ROWID = (SELECT Max(ROWID) FROM " + DATABASE_TABLE + ")",
        null);


To delete your last matching row try this:

要删除最后一个匹配的行,试试这个:

.delete(DATABASE_TABLE,
        "ROWID = (SELECT Max(ROWID) FROM " + DATABASE_TABLE + " WHERE " +
            "KEY_DATE='date' AND KEY_GRADE='style2' AND " +
            "KEY_STYLE='style' AND KEY_PUMPLEVEL='pumpLevel')",
        null);

But see my second note about SQL Injection Attacks to protect your data.

但是请参阅我关于 SQL 注入攻击以保护您的数据的第二个说明。

回答by Diego Torres Milano

You should use:

你应该使用:

Database.delete(DATABASE_TABLE,
                "KEY_DATE=? AND KEY_GRADE = ? AND KEY_STYLE = ? AND KEY_PUMPLEVEL = ?",
                 new String[]{"date", "style2", "style", "pumpLevel"});

which simplifies escaping values.

这简化了转义值。