【发布时间】:2013-08-19 22:01:05
【问题描述】:
我正在解析 JSON WebService 并创建一个数组,其中包含要在数据库中插入和删除条目的数据。
我找到了解决方案bulkInsert 使用内容提供程序中的数据库事务插入多行,但是,我正在尝试执行相同的过程来删除多行。
INSERT 解决方案:
@Override
public int bulkInsert(Uri uri, ContentValues[] allValues) {
SQLiteDatabase sqlDB = mCustomerDB.getWritableDatabase();
int numInserted = 0;
String table = MyDatabase.TABLE;
sqlDB.beginTransaction();
try {
for (ContentValues cv : allValues) {
//long newID = sqlDB.insertOrThrow(table, null, cv);
long newID = sqlDB.insertWithOnConflict(table, null, cv, SQLiteDatabase.CONFLICT_REPLACE);
if (newID <= 0) {
throw new SQLException("Error to add: " + uri);
}
}
sqlDB.setTransactionSuccessful();
getContext().getContentResolver().notifyChange(uri, null);
numInserted = allValues.length;
} finally {
sqlDB.endTransaction();
}
return numInserted;
}
使用这个调用:
mContext.getContentResolver().bulkInsert(ProviderMyDatabase.CONTENT_URI, valuesToInsertArray);
有没有办法使用内容提供者删除数据库的多行(具有此数组 ID)。
更新:
我找到了这个解决方案,使用 `IN 子句:
List<String> list = new ArrayList<String>();
for (ContentValues cv : valuesToDelete) {
Object value = cv.get(DatabaseMyDatabase.KEY_ROW_ID);
list.add(value.toString());
}
String[] args = list.toArray(new String[list.size()]);
String selection = DatabaseMyDatabase.KEY_ROW_ID + " IN(" + new String(new char[args.length-1]).replace("\0", "?,") + "?)";
int total = mContext.getContentResolver().delete(ProviderMyDatabase.CONTENT_URI, selection, args);
LOGD(TAG, "Total = " + total);
问题是,如果 JSON 返回超过 1000 行要插入,则会发生错误,因为 SQLITE_MAX_VARIABLE_NUMBER 设置为 999。它可以更改,但只能在编译时更改。
ERROR: SQLiteException: too many SQL variables
提前致谢
【问题讨论】:
标签: android database sqlite android-contentprovider android-sqlite