android SQLite 优化(三)使用事务 优化 insert
16lz
2021-01-26
SQLite 没插入一条数据默认创建一次事务
通过显示调用事务,可以提高SQLite操作的性能
String[] sCheeseNames = {"a","b","a","b","a","b","a","b"}; String[] sCheeseOrigins = {"aa","bb","aa","bb","aa","bb","aa","bb"}; SQLiteDatabase db = SQLiteDatabase.create(null); try { db.beginTransaction();//开启事务 SQLiteStatement stmt = db.compileStatement("INSERT INTO cheese VALUES(?,?)"); int i = 0; for (String name:sCheeseNames){ String origion = sCheeseOrigins[i++]; stmt.clearBindings(); stmt.bindString(1, name); stmt.bindString(2, origion); stmt.executeInsert(); } db.setTransactionSuccessful();//事务提交} catch (Exception e) {// TODO: handle exception}finally{db.endTransaction();//事务回滚}
当数据库做持久存储时,特别是存储在外部sd卡时,更应该采用一次性事务!
更多相关文章
- Android布局文件的加载过程分析:Activity.setContentView()源码
- 使用SQLiteOpenHelper 和使用ContentProvider。
- Android(安卓)访问WebService
- Android(安卓)UI- 对话框 (AlertDialog & ProgressDialog)
- 使用android画布的save()和restore()方法
- android studio中的so库调用
- Android(安卓)调用系统拍照 笔记 - 会说话的哑巴的个人页面 - 开
- Android(安卓)Binder框架实现之bindService详解
- Android(安卓)display架构分析(5-8)