android sqlite插入效率(转载)
来源:互联网 发布:云南大学学工网络系统 编辑:程序博客网 时间:2024/06/05 22:52
对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间):
1、 一个一个插入
- /**
- * 向表中插入数据
- *
- * @param openHelper
- * @param appInfo
- * @return
- */
- publicstaticboolean insert(SQLiteOpenHelper openHelper,
- RemoteAppInfo appInfo) {
- if (null == appInfo) {
- returntrue;
- }
- SQLiteDatabase db = null;
- try {
- db = openHelper.getWritableDatabase();
- ContentValues values = appInfo.getContentValues();
- return -1 != db.insert(RemoteDBHelper.TABLE_APP_REMOTE, null,
- values);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- returnfalse;
- }
- for (RemoteAppInfo remoteAppInfo : list) {
- RemoteDBUtil.insert(helper, remoteAppInfo);
- }
耗时:106524ms,也就是106s
2、 开启事务批量插入,使用
SqliteDateBase中的
insert(String table, String nullColumnHack, ContentValues values)
方法
- /**
- * 向表中插入一串数据
- *
- * @param openHelper
- * @param appInfo
- * @return 如果成功则返回true,否则返回flase
- */
- publicstaticboolean insert(SQLiteOpenHelper openHelper,
- List<RemoteAppInfo> list) {
- boolean result = true;
- if (null == list || list.size() <= 0) {
- returntrue;
- }
- SQLiteDatabase db = null;
- try {
- db = openHelper.getWritableDatabase();
- db.beginTransaction();
- for (RemoteAppInfo remoteAppInfo : list) {
- ContentValues values = remoteAppInfo.getContentValues();
- if (db.insert(RemoteDBHelper.TABLE_APP_REMOTE, null, values) < 0) {
- result = false;
- break;
- }
- }
- if (result) {
- db.setTransactionSuccessful();
- }
- } catch (Exception e) {
- e.printStackTrace();
- returnfalse;
- } finally {
- try {
- if (null != db) {
- db.endTransaction();
- db.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- returntrue;
- }
耗时:2968ms
3、 开启事务批量插入,使用
SQLiteStatement
- /**
- * 第二种方式批量插入(插入1W条数据耗时:1365ms)
- * @param openHelper
- * @param list
- * @return
- */
- publicstaticboolean insertBySql(SQLiteOpenHelper openHelper,
- List<RemoteAppInfo> list) {
- if (null == openHelper || null == list || list.size() <= 0) {
- returnfalse;
- }
- SQLiteDatabase db = null;
- try {
- db = openHelper.getWritableDatabase();
- String sql = "insert into " + RemoteDBHelper.TABLE_APP_REMOTE + "("
- + RemoteDBHelper.COL_PKG_NAME + ","// 包名
- + RemoteDBHelper.COL_USER_ACCOUNT + ","// 账号
- + RemoteDBHelper.COL_APP_SOURCE + ","// 来源
- + RemoteDBHelper.COL_SOURCE_UNIQUE + ","// PC mac 地址
- + RemoteDBHelper.COL_MOBILE_UNIQUE + ","// 手机唯一标识
- + RemoteDBHelper.COL_IMEI + ","// 手机IMEI
- + RemoteDBHelper.COL_INSTALL_STATUS + ","// 安装状态
- + RemoteDBHelper.COL_TRANSFER_RESULT + ","// 传输状态
- + RemoteDBHelper.COL_REMOTE_RECORD_ID // 唯一标识
- + ") " + "values(?,?,?,?,?,?,?,?,?)";
- SQLiteStatement stat = db.compileStatement(sql);
- db.beginTransaction();
- for (RemoteAppInfo remoteAppInfo : 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());
- long result = stat.executeInsert();
- if (result < 0) {
- returnfalse;
- }
- }
- db.setTransactionSuccessful();
- } catch (Exception e) {
- e.printStackTrace();
- returnfalse;
- } finally {
- try {
- if (null != db) {
- db.endTransaction();
- db.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- returntrue;
- }
0 0
- android sqlite插入效率(转载)
- android sqlite插入效率(转载)
- android sqlite插入效率(转载)
- android sqlite插入效率--Transaction事务应用
- SQLite提高插入效率
- android 中SQLite数据库多条插入提高执行效率
- Android:SQLite插入大量数据的效率优化
- C#:SQLite大量插入的效率问题
- sqlite插入数据效率提升解决方案
- 提升SQLITE插入、查询效率的方法
- 提升beego插入sqlite的效率
- android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
- android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
- android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
- android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
- Android SQLite 插入优化
- android 超强效率、性能优化(转载)
- Android SQLite总结[转载]
- 列表推导、字典推导、集合推导
- Web--bean类和jsp页面间的传值方式
- 打印杨辉三角系数(Pascal's Triangle / 二项式展开系数)队列实现
- 散Tips——dos窗口设置VC环境
- 数据结构-堆(heap)
- android sqlite插入效率(转载)
- 华为C语言编程风格
- JQueryEasyUI From 提交表单
- Web项目的WEB-INF目录使用说明
- 算法训练 5-1最小公倍数
- java爬取智联招聘职位信息
- iOS --- 两个NSIndexPath对象的正确比较方式
- 正式Demo-和风天气
- 解析从Mysql导出的JSON文件