gpt4 book ai didi

android - 如何在 android 中使用 bulkInsert() 函数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:58 25 4
gpt4 key购买 nike

在所有批量插入数据库完成后,我只需要一个通知。请提供使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com