如何在Android中使用bulkInsert()函数? [英] How to use bulkInsert() function in android?

查看:234
本文介绍了如何在Android中使用bulkInsert()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want only a single notification after all the bulk insertion is done into database. Please provide an example to use bulkInsert() function. I cannot find a proper example on internet. Please Help!!!!

推荐答案

这是使用ContentProvider的bulkInsert.

This is bulkInsert using ContentProvider.

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时,仅调用一次.

Call it only once when you will have more ContentValues in ContentValues[] values array.

这篇关于如何在Android中使用bulkInsert()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆