android数据库 批量 事务 操作

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


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

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


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


4.事务写法:

1. 使用db.execSQL(sql)
  1. public void inertOrUpdateDateBatch(List<String> sqls) {     
  2.         SQLiteDatabase db = getWritableDatabase();     
  3.         db.beginTransaction();     
  4.         try {     
  5.             for (String sql : sqls) {     
  6.                 db.execSQL(sql);     
  7.             }     
  8.             // 设置事务标志为成功,当结束事务时就会提交事务     
  9.             db.setTransactionSuccessful();     
  10.         } catch (Exception e) {     
  11.             e.printStackTrace();     
  12.         } finally {     
  13.             // 结束事务     
  14.             db.endTransaction();     
  15.             db.close();     
  16.         }     
  17. }   

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

  1. db.beginTransaction(); // 手动设置开始事务  
  2.        for (ContentValues v : list) {  
  3.            db.insert("bus_line_station"null, v);  
  4.        }  
  5.        db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交  
  6.        db.endTransaction(); // 处理完成  
  7.   
  8.        db.close(); 
    3. 使用InsertHelper类

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

    1. InsertHelper ih = new InsertHelper(db, "bus_line_station");  
    2.         db.beginTransaction();  
    3.         final int directColumnIndex = ih.getColumnIndex("direct");  
    4.         final int lineNameColumnIndex = ih.getColumnIndex("line_name");  
    5.         final int snoColumnIndex = ih.getColumnIndex("sno");  
    6.         final int stationNameColumnIndex = ih.getColumnIndex("station_name");  
    7.         try {  
    8.             for (Station s : busLines) {  
    9.                 ih.prepareForInsert();  
    10.                 ih.bind(directColumnIndex, s.direct);  
    11.                 ih.bind(lineNameColumnIndex, s.lineName);  
    12.                 ih.bind(snoColumnIndex, s.sno);  
    13.                 ih.bind(stationNameColumnIndex, s.stationName);  
    14.                 ih.execute();  
    15.             }  
    16.             db.setTransactionSuccessful();  
    17.         } finally {  
    18.             ih.close();  
    19.             db.endTransaction();  
    20.             db.close();  
    21.         }  


    4. 使用SQLiteStatement

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

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

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

    运行结果

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值