对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间):

1、 一个一个插入

Java代码
  1. /**
  2. *向表中插入数据
  3. *
  4. *@paramopenHelper
  5. *@paramappInfo
  6. *@return
  7. */
  8. publicstaticbooleaninsert(SQLiteOpenHelperopenHelper,
  9. RemoteAppInfoappInfo){
  10. if(null==appInfo){
  11. returntrue;
  12. }
  13. SQLiteDatabasedb=null;
  14. try{
  15. db=openHelper.getWritableDatabase();
  16. ContentValuesvalues=appInfo.getContentValues();
  17. return-1!=db.insert(RemoteDBHelper.TABLE_APP_REMOTE,null,
  18. values);
  19. }catch(Exceptione){
  20. e.printStackTrace();
  21. }finally{
  22. if(null!=db){
  23. db.close();
  24. }
  25. }
  26. returnfalse;
  27. }
  28. for(RemoteAppInforemoteAppInfo:list){
  29. RemoteDBUtil.insert(helper,remoteAppInfo);
  30. }

耗时:106524ms,也就是106s

2、 开启事务批量插入,使用

SqliteDateBase中的

insert(String table, String nullColumnHack, ContentValues values)

方法

Java代码
  1. /**
  2. *向表中插入一串数据
  3. *
  4. *@paramopenHelper
  5. *@paramappInfo
  6. *@return如果成功则返回true,否则返回flase
  7. */
  8. publicstaticbooleaninsert(SQLiteOpenHelperopenHelper,
  9. List<RemoteAppInfo>list){
  10. booleanresult=true;
  11. if(null==list||list.size()<=0){
  12. returntrue;
  13. }
  14. SQLiteDatabasedb=null;
  15. try{
  16. db=openHelper.getWritableDatabase();
  17. db.beginTransaction();
  18. for(RemoteAppInforemoteAppInfo:list){
  19. ContentValuesvalues=remoteAppInfo.getContentValues();
  20. if(db.insert(RemoteDBHelper.TABLE_APP_REMOTE,null,values)<0){
  21. result=false;
  22. break;
  23. }
  24. }
  25. if(result){
  26. db.setTransactionSuccessful();
  27. }
  28. }catch(Exceptione){
  29. e.printStackTrace();
  30. returnfalse;
  31. }finally{
  32. try{
  33. if(null!=db){
  34. db.endTransaction();
  35. db.close();
  36. }
  37. }catch(Exceptione){
  38. e.printStackTrace();
  39. }
  40. }
  41. returntrue;
  42. }

耗时:2968ms

3、开启事务批量插入,使用

SQLiteStatement

Java代码
  1. /**
  2. *第二种方式批量插入(插入1W条数据耗时:1365ms)
  3. *@paramopenHelper
  4. *@paramlist
  5. *@return
  6. */
  7. publicstaticbooleaninsertBySql(SQLiteOpenHelperopenHelper,
  8. List<RemoteAppInfo>list){
  9. if(null==openHelper||null==list||list.size()<=0){
  10. returnfalse;
  11. }
  12. SQLiteDatabasedb=null;
  13. try{
  14. db=openHelper.getWritableDatabase();
  15. Stringsql="insertinto"+RemoteDBHelper.TABLE_APP_REMOTE+"("
  16. +RemoteDBHelper.COL_PKG_NAME+","//包名
  17. +RemoteDBHelper.COL_USER_ACCOUNT+","//账号
  18. +RemoteDBHelper.COL_APP_SOURCE+","//来源
  19. +RemoteDBHelper.COL_SOURCE_UNIQUE+","//PCmac地址
  20. +RemoteDBHelper.COL_MOBILE_UNIQUE+","//手机唯一标识
  21. +RemoteDBHelper.COL_IMEI+","//手机IMEI
  22. +RemoteDBHelper.COL_INSTALL_STATUS+","//安装状态
  23. +RemoteDBHelper.COL_TRANSFER_RESULT+","//传输状态
  24. +RemoteDBHelper.COL_REMOTE_RECORD_ID//唯一标识
  25. +")"+"values(?,?,?,?,?,?,?,?,?)";
  26. SQLiteStatementstat=db.compileStatement(sql);
  27. db.beginTransaction();
  28. for(RemoteAppInforemoteAppInfo:list){
  29. stat.bindString(1,remoteAppInfo.getPkgName());
  30. stat.bindString(2,remoteAppInfo.getAccount());
  31. stat.bindLong(3,remoteAppInfo.getFrom());
  32. stat.bindString(4,remoteAppInfo.getFromDeviceMd5());
  33. stat.bindString(5,remoteAppInfo.getMoblieMd5());
  34. stat.bindString(6,remoteAppInfo.getImei());
  35. stat.bindLong(7,remoteAppInfo.getInstallStatus());
  36. stat.bindLong(8,remoteAppInfo.getTransferResult());
  37. stat.bindString(9,remoteAppInfo.getRecordId());
  38. longresult=stat.executeInsert();
  39. if(result<0){
  40. returnfalse;
  41. }
  42. }
  43. db.setTransactionSuccessful();
  44. }catch(Exceptione){
  45. e.printStackTrace();
  46. returnfalse;
  47. }finally{
  48. try{
  49. if(null!=db){
  50. db.endTransaction();
  51. db.close();
  52. }
  53. }catch(Exceptione){
  54. e.printStackTrace();
  55. }
  56. }
  57. returntrue;
  58. }

耗时:1365ms

更多相关文章

  1. Android(安卓)studio爬取网页
  2. android数据库使用小结
  3. 网上找了些Android数据库操作的代码
  4. 【工具类】Android判断SD卡状态
  5. Android(安卓)APP 启动时间统计
  6. Android将发送的短信插入数据库
  7. Android之数据库
  8. Android中向ContactsProvider中插入大量联系人
  9. Android开发学习笔记(十三) ProgressDialog学习

随机推荐

  1. 匿名类中this的特殊用法(class.this)
  2. Android JNI 之 Bitmap 操作
  3. android的apk包中的xml文件打开方法
  4. android 利用重力感应监测手机摇晃,做出相
  5. robotium获取本地文档内容
  6. AndroidUi(1)-直线
  7. ubuntu 使用android studio 查看android(
  8. Android开发的小细节
  9. MTK 平台(MTK6573)马达驱动
  10. android解决坚屏拍照和保存图片旋转90度