gpt4 book ai didi

android - ContentResolver.bulkInsert(..) 有什么意义?

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

我的目标

我想批量(事务性)将多条记录插入到 sqlite 中。

我的问题

我找到了 android.content.ContentResolver 方法。 bulkInsert (..) 有趣但 javadoc 指出:

This function make no guarantees about the atomicity of the insertions.

为什么 android 会提供一个残废的方法?你能说出非原子插入的用例吗?我显然要覆盖 ContentProvider.bulkInsert(..) 以确保我自己的原子性,所以我不确定为什么它是这样的短语。

最佳答案

我们需要重写批量插入方法,如下所示...

public class Provider extends ContentProvider {
public static final Uri URI = Uri.parse("content://com.example.android.hoge/");
@Override
public String getType(Uri uri) {
return null;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getReadableDatabase();
Cursor cursor = sdb.query(
Table.TABLENAME,
new String[]{Table.ID, Table.DATA, Table.CREATED},
selection,
selectionArgs,
null,
null,
sortOrder,
null
);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
sdb.insert(Table.TABLENAME, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}

/**
* super.bulkInsert is implemented the loop of insert without transaction
* So we need to override it and implement transaction.
*/
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();

sdb.beginTransaction();
SQLiteStatement stmt = sdb.compileStatement(
"INSERT INTO `" + Table.TABLENAME + "`(`" + Table.DATA + "`, `" + Table.CREATED + "`) VALUES (?, ?);"
);
int length = values.length;
for(int i = 0; i < length; i++){
stmt.bindString(1, values[i].getAsString(Table.DATA));
stmt.bindLong(2, values[i].getAsLong(Table.CREATED));
stmt.executeInsert();
}
sdb.setTransactionSuccessful();
sdb.endTransaction();
return length;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
int rows = sdb.update(Table.TABLENAME, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rows;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Helper helper = Helper.getInstance(getContext(), null);
SQLiteDatabase sdb = helper.getWritableDatabase();
int rows = sdb.delete(Table.TABLENAME, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rows;
}

private static class Helper extends SQLiteOpenHelper {
static Helper INSTANCE = null;
private Helper(Context context, CursorFactory factory) {
super(context, Table.FILENAME, factory, Table.VERSION);
}
public static Helper getInstance(Context context, CursorFactory factory) {
if (INSTANCE == null) {
INSTANCE = new Helper(context, factory);
}
return INSTANCE;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE `" + Table.TABLENAME + "`(" +
" `" + Table.ID + "` INTEGER PRIMARY KEY AUTOINCREMENT," +
" `" + Table.CREATED + "` INTEGER," +
" `" + Table.DATA + "` TEXT" +
");"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}

关于android - ContentResolver.bulkInsert(..) 有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725593/

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