android数据库 批量 事务 操作

先说说多线程 数据库sqlite问题:


1:多线程 单例 SQLiteOpenHelper 可以同时读和写。

2.多线程 多个对象 SQLiteOpenHelper  不能同时读写(可以同时读)。


3.多线程事务(1个读写过程就是一个事务,推荐以后都用事务的方式,对于大量的数据)


4.事务写法:

1. 使用db.execSQL(sql)
public void inertOrUpdateDateBatch(List<String> sqls) {   
        SQLiteDatabase db = getWritableDatabase();   
        db.beginTransaction();   
        try {   
            for (String sql : sqls) {   
                db.execSQL(sql);   
            }   
            // 设置事务标志为成功,当结束事务时就会提交事务   
            db.setTransactionSuccessful();   
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {   
            // 结束事务   
            db.endTransaction();   
            db.close();   
        }   
} 

2. 使用db.insert("table_name", null, contentValues)

 db.beginTransaction(); // 手动设置开始事务
        for (ContentValues v : list) {
            db.insert("bus_line_station", null, v);
        }
        db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
        db.endTransaction(); // 处理完成

        db.close();

3. 使用InsertHelper类

这个类在API 17中已经被废弃了,参考博客:http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/

InsertHelper ih = new InsertHelper(db, "bus_line_station");
        db.beginTransaction();
        final int directColumnIndex = ih.getColumnIndex("direct");
        final int lineNameColumnIndex = ih.getColumnIndex("line_name");
        final int snoColumnIndex = ih.getColumnIndex("sno");
        final int stationNameColumnIndex = ih.getColumnIndex("station_name");
        try {
            for (Station s : busLines) {
                ih.prepareForInsert();
                ih.bind(directColumnIndex, s.direct);
                ih.bind(lineNameColumnIndex, s.lineName);
                ih.bind(snoColumnIndex, s.sno);
                ih.bind(stationNameColumnIndex, s.stationName);
                ih.execute();
            }
            db.setTransactionSuccessful();
        } finally {
            ih.close();
            db.endTransaction();
            db.close();
        }


4. 使用SQLiteStatement

查看InsertHelper时,官方文档提示改类已经废弃,请使用SQLiteStatement,链接:https://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html 该方法类似于JDBC里面的预编译sql语句,使用方法如下:

String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";
        SQLiteStatement stat = db.compileStatement(sql);
        db.beginTransaction();
        for (Station line : busLines) {
            stat.bindLong(1, line.direct);
            stat.bindString(2, line.lineName);
            stat.bindLong(3, line.sno);
            stat.bindString(4, line.stationName);
            stat.executeInsert();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

下图是以上4中方法在批量插入1万条数据消耗的时间

运行结果

可以发现第三种方法需要的时间最短,鉴于该类已经在API17中废弃,所以第四种方法应该是最优的方法。


  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值