1. 使用db.execSQL(sql)
这里是把要插入的数据拼接成可执行的sql语句,然后调用db.execSQL(sql)方法执行插入
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)
这里是把要插入的数据封装到ContentValues类中,然后调用db.isert()方法执行插入,
db.beginTransaction(); // 手动设置开始事务
for (ContentValues v : list) {
db.insert("bus_line_station", null, v);
}
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 处理完成
db.close();
3. 使用InsertHelper类
这个类在API 17中已经被废弃了,参考博客:
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,
该方法类似于JDBC里面的预编译sql语句,使用方法如下:
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万条数据消耗的时间
最近在做android项目的时候遇到一个问题,应用程序初始化时需要批量的向sqlite中插入大量数,导致应用启动过慢。
android使用的是sqlite数据库,sqlite是比较轻量级的数据库,在Google了之后发现,sqlite事务处理的问题,
在sqlite插入数据的时候默认一条语句就是一个事务,有多少条数据就有多少次磁盘操作。我的应用初始5000条记录也就是要5000次读写磁盘操作。
解决方法:
添加事务处理,把5000条插入作为一个事务
dataBase.beginTransaction(); //手动设置开始事务
//数据插入操作循环
dataBase.setTransactionSuccessful(); //设置事务处理成功,不设置会自动回滚不提交
dataBase.endTransaction(); //处理完成
这里是把要插入的数据拼接成可执行的sql语句,然后调用db.execSQL(sql)方法执行插入
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)
这里是把要插入的数据封装到ContentValues类中,然后调用db.isert()方法执行插入,
db.beginTransaction(); // 手动设置开始事务
for (ContentValues v : list) {
db.insert("bus_line_station", null, v);
}
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
db.endTransaction(); // 处理完成
db.close();
3. 使用InsertHelper类
这个类在API 17中已经被废弃了,参考博客:
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,
该方法类似于JDBC里面的预编译sql语句,使用方法如下:
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万条数据消耗的时间
最近在做android项目的时候遇到一个问题,应用程序初始化时需要批量的向sqlite中插入大量数,导致应用启动过慢。
android使用的是sqlite数据库,sqlite是比较轻量级的数据库,在Google了之后发现,sqlite事务处理的问题,
在sqlite插入数据的时候默认一条语句就是一个事务,有多少条数据就有多少次磁盘操作。我的应用初始5000条记录也就是要5000次读写磁盘操作。
解决方法:
添加事务处理,把5000条插入作为一个事务
dataBase.beginTransaction(); //手动设置开始事务
//数据插入操作循环
dataBase.setTransactionSuccessful(); //设置事务处理成功,不设置会自动回滚不提交
dataBase.endTransaction(); //处理完成