SQLite的更新具有参数化IN(...)子句子句、参数、SQLite、IN

2023-09-04 04:21:47 作者:口含薄荷〞心微凉

在我的应用我正与SQLite数据库 - 我打了一个奇怪的问题

In my app I'm working with sqlite database - and I hit a strange issue.

我有一个看起来像这样的表:

I have a table that looks like this:

_id   field1   field2   status
---   ------   ------   ------
 1    value    value    ...
 2    value    value    ...
...

在一个地方,我需要更新一些行将它们设置为另一种状态,所以我想

At one place, I need to update some rows to set them to another status, so I'm trying

SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("status", STATUS_SENT);
Set<Integer> ids = datasource.getCompletedIds();
String idparam = TextUtils.join(",", ids);
int cnt = db.update(TABLE_ORDERS, cv, "_id in (?)", new String[] { idparam });
Log.d("db", "updated " + cnt + " rows");
db.close();

但是没有得到更新 - 和 db.update 返回0。我在想什么

However nothing gets updated - and db.update returns 0. What am I missing?

推荐答案

我不开心推荐的没有的使用参数,但是这种情况下,它实际上不是格式所有的 标记(与普通整数,就像安全):

I'm not happy recommending not using parameters, but it this case, it's actually easier than formatting all those ? markers (and with plain integers, just as safe):

db.update(TABLE_ORDERS, cv, "_id IN (" + idparam + ")", null);
 
精彩推荐
图片推荐