android批量插入数据效率对比
16lz
2022-06-11
对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间):
1、 一个一个插入
Java代码- /**
- *向表中插入数据
- *
- *@paramopenHelper
- *@paramappInfo
- *@return
- */
- publicstaticbooleaninsert(SQLiteOpenHelperopenHelper,
- RemoteAppInfoappInfo){
- if(null==appInfo){
- returntrue;
- }
- SQLiteDatabasedb=null;
- try{
- db=openHelper.getWritableDatabase();
- ContentValuesvalues=appInfo.getContentValues();
- return-1!=db.insert(RemoteDBHelper.TABLE_APP_REMOTE,null,
- values);
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- if(null!=db){
- db.close();
- }
- }
- returnfalse;
- }
- for(RemoteAppInforemoteAppInfo:list){
- RemoteDBUtil.insert(helper,remoteAppInfo);
- }
耗时:106524ms,也就是106s
2、 开启事务批量插入,使用
SqliteDateBase中的
insert(String table, String nullColumnHack, ContentValues values)
方法
Java代码- /**
- *向表中插入一串数据
- *
- *@paramopenHelper
- *@paramappInfo
- *@return如果成功则返回true,否则返回flase
- */
- publicstaticbooleaninsert(SQLiteOpenHelperopenHelper,
- List<RemoteAppInfo>list){
- booleanresult=true;
- if(null==list||list.size()<=0){
- returntrue;
- }
- SQLiteDatabasedb=null;
- try{
- db=openHelper.getWritableDatabase();
- db.beginTransaction();
- for(RemoteAppInforemoteAppInfo:list){
- ContentValuesvalues=remoteAppInfo.getContentValues();
- if(db.insert(RemoteDBHelper.TABLE_APP_REMOTE,null,values)<0){
- result=false;
- break;
- }
- }
- if(result){
- db.setTransactionSuccessful();
- }
- }catch(Exceptione){
- e.printStackTrace();
- returnfalse;
- }finally{
- try{
- if(null!=db){
- db.endTransaction();
- db.close();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- returntrue;
- }
耗时:2968ms
3、开启事务批量插入,使用
SQLiteStatement
Java代码- /**
- *第二种方式批量插入(插入1W条数据耗时:1365ms)
- *@paramopenHelper
- *@paramlist
- *@return
- */
- publicstaticbooleaninsertBySql(SQLiteOpenHelperopenHelper,
- List<RemoteAppInfo>list){
- if(null==openHelper||null==list||list.size()<=0){
- returnfalse;
- }
- SQLiteDatabasedb=null;
- try{
- db=openHelper.getWritableDatabase();
- Stringsql="insertinto"+RemoteDBHelper.TABLE_APP_REMOTE+"("
- +RemoteDBHelper.COL_PKG_NAME+","//包名
- +RemoteDBHelper.COL_USER_ACCOUNT+","//账号
- +RemoteDBHelper.COL_APP_SOURCE+","//来源
- +RemoteDBHelper.COL_SOURCE_UNIQUE+","//PCmac地址
- +RemoteDBHelper.COL_MOBILE_UNIQUE+","//手机唯一标识
- +RemoteDBHelper.COL_IMEI+","//手机IMEI
- +RemoteDBHelper.COL_INSTALL_STATUS+","//安装状态
- +RemoteDBHelper.COL_TRANSFER_RESULT+","//传输状态
- +RemoteDBHelper.COL_REMOTE_RECORD_ID//唯一标识
- +")"+"values(?,?,?,?,?,?,?,?,?)";
- SQLiteStatementstat=db.compileStatement(sql);
- db.beginTransaction();
- for(RemoteAppInforemoteAppInfo:list){
- stat.bindString(1,remoteAppInfo.getPkgName());
- stat.bindString(2,remoteAppInfo.getAccount());
- stat.bindLong(3,remoteAppInfo.getFrom());
- stat.bindString(4,remoteAppInfo.getFromDeviceMd5());
- stat.bindString(5,remoteAppInfo.getMoblieMd5());
- stat.bindString(6,remoteAppInfo.getImei());
- stat.bindLong(7,remoteAppInfo.getInstallStatus());
- stat.bindLong(8,remoteAppInfo.getTransferResult());
- stat.bindString(9,remoteAppInfo.getRecordId());
- longresult=stat.executeInsert();
- if(result<0){
- returnfalse;
- }
- }
- db.setTransactionSuccessful();
- }catch(Exceptione){
- e.printStackTrace();
- returnfalse;
- }finally{
- try{
- if(null!=db){
- db.endTransaction();
- db.close();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- returntrue;
- }
耗时:1365ms
更多相关文章
- Android(安卓)studio爬取网页
- android数据库使用小结
- 网上找了些Android数据库操作的代码
- 【工具类】Android判断SD卡状态
- Android(安卓)APP 启动时间统计
- Android将发送的短信插入数据库
- Android之数据库
- Android中向ContactsProvider中插入大量联系人
- Android开发学习笔记(十三) ProgressDialog学习