android数据库 批量 事务 操作
2015-10-11 10:02
465 查看
先说说多线程 数据库sqlite问题:
1:多线程 单例 SQLiteOpenHelper 可以同时读和写。
2.多线程 多个对象 SQLiteOpenHelper 不能同时读写(可以同时读)。
3.多线程事务(1个读写过程就是一个事务,推荐以后都用事务的方式,对于大量的数据)
4.事务写法:
[java] view
plaincopy
public void inertOrUpdateDateBatch(List<String> sqls) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
for (String sql : sqls) {
db.execSQL(sql);
}
// 设置事务标志为成功,当结束事务时就会提交事务
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 结束事务
db.endTransaction();
db.close();
}
}
[java] view
plaincopy
db.beginTransaction(); // 手动设置开始事务
for (ContentValues v : list) {
db.insert("bus_line_station", null, v);
}
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 处理完成
db.close();
这个类在API 17中已经被废弃了,参考博客:http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/
[java] view
plaincopy
InsertHelper ih = new InsertHelper(db, "bus_line_station");
db.beginTransaction();
final int directColumnIndex = ih.getColumnIndex("direct");
final int lineNameColumnIndex = ih.getColumnIndex("line_name");
final int snoColumnIndex = ih.getColumnIndex("sno");
final int stationNameColumnIndex = ih.getColumnIndex("station_name");
try {
for (Station s : busLines) {
ih.prepareForInsert();
ih.bind(directColumnIndex, s.direct);
ih.bind(lineNameColumnIndex, s.lineName);
ih.bind(snoColumnIndex, s.sno);
ih.bind(stationNameColumnIndex, s.stationName);
ih.execute();
}
db.setTransactionSuccessful();
} finally {
ih.close();
db.endTransaction();
db.close();
}
查看InsertHelper时,官方文档提示改类已经废弃,请使用SQLiteStatement,链接:https://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html 该方法类似于JDBC里面的预编译sql语句,使用方法如下:
[java] view
plaincopy
String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";
SQLiteStatement stat = db.compileStatement(sql);
db.beginTransaction();
for (Station line : busLines) {
stat.bindLong(1, line.direct);
stat.bindString(2, line.lineName);
stat.bindLong(3, line.sno);
stat.bindString(4, line.stationName);
stat.executeInsert();
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
可以发现第三种方法需要的时间最短,鉴于该类已经在API17中废弃,所以第四种方法应该是最优的方法。
1:多线程 单例 SQLiteOpenHelper 可以同时读和写。
2.多线程 多个对象 SQLiteOpenHelper 不能同时读写(可以同时读)。
3.多线程事务(1个读写过程就是一个事务,推荐以后都用事务的方式,对于大量的数据)
4.事务写法:
1. 使用db.execSQL(sql)
[java] viewplaincopy
public void inertOrUpdateDateBatch(List<String> sqls) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
for (String sql : sqls) {
db.execSQL(sql);
}
// 设置事务标志为成功,当结束事务时就会提交事务
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 结束事务
db.endTransaction();
db.close();
}
}
2. 使用db.insert("table_name", null, contentValues)
[java] viewplaincopy
db.beginTransaction(); // 手动设置开始事务
for (ContentValues v : list) {
db.insert("bus_line_station", null, v);
}
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 处理完成
db.close();
3. 使用InsertHelper类
这个类在API 17中已经被废弃了,参考博客:http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/[java] view
plaincopy
InsertHelper ih = new InsertHelper(db, "bus_line_station");
db.beginTransaction();
final int directColumnIndex = ih.getColumnIndex("direct");
final int lineNameColumnIndex = ih.getColumnIndex("line_name");
final int snoColumnIndex = ih.getColumnIndex("sno");
final int stationNameColumnIndex = ih.getColumnIndex("station_name");
try {
for (Station s : busLines) {
ih.prepareForInsert();
ih.bind(directColumnIndex, s.direct);
ih.bind(lineNameColumnIndex, s.lineName);
ih.bind(snoColumnIndex, s.sno);
ih.bind(stationNameColumnIndex, s.stationName);
ih.execute();
}
db.setTransactionSuccessful();
} finally {
ih.close();
db.endTransaction();
db.close();
}
4. 使用SQLiteStatement
查看InsertHelper时,官方文档提示改类已经废弃,请使用SQLiteStatement,链接:https://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html 该方法类似于JDBC里面的预编译sql语句,使用方法如下:[java] view
plaincopy
String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";
SQLiteStatement stat = db.compileStatement(sql);
db.beginTransaction();
for (Station line : busLines) {
stat.bindLong(1, line.direct);
stat.bindString(2, line.lineName);
stat.bindLong(3, line.sno);
stat.bindString(4, line.stationName);
stat.executeInsert();
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
下图是以上4中方法在批量插入1万条数据消耗的时间
可以发现第三种方法需要的时间最短,鉴于该类已经在API17中废弃,所以第四种方法应该是最优的方法。
相关文章推荐
- Android中shape的使用
- Android 通过solid来定义不同边框的颜色,可以只定义一个边框的颜色
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事项
- Android Api Demos登顶之路(九十三)Graphics-->Vertices
- Android ListView标题置顶效果实现
- [转]Android:Activity+Fragment及它们之间的数据交换(一)
- Android 使用Notification自定义通知栏显示
- Android中设计模式之单例模式的种类
- Android viewpage滑动选项卡
- android 开发时遇到的问题
- Android应用开发性能优化完全分析
- Android Api Demos登顶之路(九十二)Graphics-->TypeFace
- Android ViewPager前两页显示空白
- Android应用开发性能优化完全分析
- 安卓手机烧内存卡(就是内存卡损坏)的补救办法
- Android学习实践:10.单选按钮RadioButton
- Android 学习之路一:Activity之间传递数据的四种方式
- Android 学习之路 1:如何在Activity之间传递数据
- Android中常见的设计模式
- android反编译