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

问题描述 投票:8回答:3

在将所有批量插入完成到数据库之后,我只想要一个通知。请提供一个使用bulkInsert()函数的示例。我在互联网上找不到合适的例子。请帮忙!!!!

android android-contentprovider bulkinsert
3个回答
38
投票

这是使用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 [] values数组中有更多ContentValues时,只调用一次。


4
投票

我一直在寻找一个在活动方面和内容提供者方面实现这个目的的教程。我使用了上面的“术士”答案,它在内容提供商方面表现很好。我使用this post的答案在活动结束时准备ContentValues数组。我还修改了我的ContentValues以接收一串逗号分隔值(或新行,句号,半冒号)。看起来像这样:

ContentValues[] bulkToInsert;
List<ContentValues>mValueList = new ArrayList<ContentValues>();

String regexp = "[,;.\\n]+"; // delimiters without space or tab
//String regexp = "[\\s,;.\\n\\t]+"; // delimiters with space and tab
List<String> splitStrings = Arrays.asList(stringToSplit.split(regexp));

for (String temp : splitStrings) {
    Log.d("current student name being put: ", temp);
    ContentValues mNewValues = new ContentValues();
    mNewValues.put(Contract.KEY_STUDENT_NAME, temp );
    mNewValues.put(Contract.KEY_GROUP_ID, group_id);
    mValueList.add(mNewValues);
}
bulkToInsert = new ContentValues[mValueList.size()];
mValueList.toArray(bulkToInsert);
getActivity().getContentResolver().bulkInsert(Contract.STUDENTS_CONTENT_URI, bulkToInsert);

我找不到更简洁的方法将描绘的分割字符串直接附加到bulkInsert的ContentValues数组。但这一功能直到我找到它。


0
投票

试试这种方法。

public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    switch (sUriMatcher.match(uri)) {
         case CODE_WEATHER:
            db.beginTransaction();
            int rowsInserted = 0;
            try {
                for (ContentValues value : values) {
                    long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);
                    if (_id != -1) {
                        rowsInserted++;
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
            if (rowsInserted > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return rowsInserted;
        default:
            return super.bulkInsert(uri, values);
    }
}

术士的答案插入全部或无行。此外,在setTransactionSuccessful()endTransaction()之间执行最小的任务,当然这两个函数调用之间没有数据库操作。

代码来源:Udacity

© www.soinside.com 2019 - 2024. All rights reserved.