Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除SQLITE(android)中的多行_Android_Sqlite - Fatal编程技术网

删除SQLITE(android)中的多行

删除SQLITE(android)中的多行,android,sqlite,Android,Sqlite,我想删除表中与数组中的ID匹配的所有行。我可以通过以下两种方法中的任何一种(两种方法都有效)。你能告诉我哪一个更好吗 方法1: public void deleteRec(String[] ids) { //ids is an array SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_NAME, KEY_ID+" IN (" + new String(new char[ids.len

我想删除表中与数组中的ID匹配的所有行。我可以通过以下两种方法中的任何一种(两种方法都有效)。你能告诉我哪一个更好吗

方法1:

public void deleteRec(String[] ids) { //ids is an array
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, KEY_ID+" IN (" + new String(new char[ids.length-1]).replace("\0", "?,") + "?)", ids);
        db.close();
    }
方法2:

public void deleteRec(String[] ids) { //ids is an array
        String allid = TextUtils.join(", ", ids);
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(String.format("DELETE FROM "+TABLE_NAME+" WHERE "+KEY_ID+" IN (%s);", allid));
       db.close();
    }
读了这篇文章,它说您不应该将execSQL与INSERT、DELETE、UPDATE或SELECT一起使用,因为这可能会带来潜在的安全风险。而且我仍然相信关于性能,第一个更好

试试这个

public void deleteRec(String[] ids) { //ids is an array
    SQLiteDatabase db = this.getWritableDatabase();
    for (int i = 0; i < ids.length; i++) {
        //Your code for delete
    }
    db.close();
}
public void deleteRec(String[]ids){//ids是一个数组
SQLiteDatabase db=this.getWritableDatabase();
for(int i=0;i
忘了第二种方法吧

您的
id
都是来自数字的字符串(否则SQL将失败),但是对于一般的字符串数据,将数据传递到SQL语句中从来都不是一个好主意。使应用程序易受SQL注入攻击:

String.format("DELETE FROM t WHERE ID='%s', "1' AND 1=1 --")
// = "DELETE FROM t WHERE ID='1' AND 1=1 --'" => would delete all data!
并且可能会使SQL语句失败:

String.format("DELETE FROM t WHERE v='%s', "It's me!")
// = "DELETE FROM t WHERE v='It's me!'" => syntactically incorrect (quote not escaped)!
编辑:由于
ID
作为字符串数组提供,并且可能
KEY\u ID
引用了
INT
列,因此方法1应适用于:

db.delete(TABLE_NAME, "CAST("+KEY_ID+" AS TEXT) IN (" + new String(new char[ids.length-1]).replace("\0", "?,") + "?)", ids);

试试这个Maybay它可以帮助你:

String[] Ids = ......; //Array of Ids you wish to delete.
String whereClause = String.format(COL_NAME + " in (%s)", new Object[] { TextUtils.join(",", Collections.nCopies(Ids.length, "?")) });
db.delete(TABLE_NAME, whereClause, Ids);
我用了这个:

String[] Ids = new String[]{"1", "2", "3"};
mContext.getContentResolver().delete(CONTENT_URI, ID + " IN (?, ?, ?)",Ids);

多重?工作。

为了可读性,我更喜欢第二种方法。但就性能而言,我认为这不会对sqlite事务产生太大影响。请阅读有关sqlite事务的内容。第二种方法可以更改为
db.delete(TABLE_NAME,KEY_ID+“IN(?)”,新字符串[]{allid})以跟随文档。@antimo。。。这很好,而且是复合的。。我会给你一个建议try@antimo不,它不能!!!关于将数组放入(…)
子句中的
,有很多问题。我想我真的错了。
allid
将假定为1个值,因为它是由
query()
转义的。对不起,这与安全风险无关;
insert
/
update
/
delete
函数只能返回像
ExecSQL
这样的通用函数无法提供的信息。如果我有50个ID,它将访问数据库50次。不确定这是否是一个好主意。这并不能回答问题,而且比两者都糟糕。正如文档(,java.lang.String,java.lang.String[])所述:“值将被绑定为字符串。”这意味着您指出的注入将不起作用,它将被解释为文本,而不会被解析。@AndrasBalázsLajtha Right,但是我更愿意使用
CAST
将字符串连接到SQL中。表达式
new String(new char[ids.length-1]).replace(“\0”,“?,”)
应该得到一个解释:它创建一个带有默认空值(
\0
)的
字符数组,然后用
替换这些空值?,
,这就是我喜欢的。。。。。它通过单个步骤减少了每个循环的写操作。。。最终都是一样的。。创造动态?在where子句中。