在android中的Sqlite数据库更新命令中无法更新多行

时间:2010-12-30 07:00:26

标签: android sqlite

我在这里面临一个问题。我试图批量更新我的表,即尝试更新数据库中的多个行。更新很简单。我只需要将列值设置为1,它实际上在应用程序中用作标志。所以我想将值设置为1,在where子句中,我给出了我想要设置的所有id的字符串数组。 但问题是它给了我一个“android.database.sqlite.SQLiteException:绑定或列索引超出范围:”。但是,当以字符串或字符串数​​组提供单个值时,更新可以正常工作。

这是查询

db.update( tableName, cv, Z_ID + "=?", items );

其中items的类型为String []

请告诉我我错过了什么?

问候

法赫德·阿里·谢赫

1 个答案:

答案 0 :(得分:4)

根据你给出的一条线,这是行不通的。

如果我们查看方法调用签名:

update(String table, ContentValues values, String whereClause, String[] whereArgs)

这样:

ContentValues cv=new ContentValues(); cv.put(colName, new Integer(1));

String[] items = new String []{String.valueOf(Z_ID)}

db.update( tableName, cv, Z_ID + "=?", items );

你的代码看起来像这样吗?

您的items数组必须与where子句匹配。一个不同的例子看起来像这样:

ContentValues cv=new ContentValues(); cv.put(salesColName, new Integer(4534));

String whereClause = "band=? and album=?"; String whereArgs = new String [] { "U2", "Joshua Tree" };

db.update( tableName, cv, whereClause , whereArgs);
相关问题