在所有批量插入数据库完成后,我只需要一个通知。 请提供使用 bulkInsert() 函数的示例。 我在互联网上找不到合适的例子。请帮助!!!!
最佳答案
这是使用 ContentProvider 的 bulkInsert。
public int bulkInsert(Uri uri, ContentValues[] values){
int numInserted = 0;
String table;
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case PEOPLE:
table = TABLE_PEOPLE;
break;
}
SQLiteDatabase sqlDB = database.getWritableDatabase();
sqlDB.beginTransaction();
try {
for (ContentValues cv : values) {
long newID = sqlDB.insertOrThrow(table, null, cv);
if (newID <= 0) {
throw new SQLException("Failed to insert row into " + uri);
}
}
sqlDB.setTransactionSuccessful();
getContext().getContentResolver().notifyChange(uri, null);
numInserted = values.length;
} finally {
sqlDB.endTransaction();
}
return numInserted;
}
当 ContentValues[] 值数组中有更多 ContentValues 时,只调用一次。
关于android - 如何在 android 中使用 bulkInsert() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12730908/