最近由于项目的需要,需要提高项目中数据的入库性能,通过优化,发现入库方法不同,效率真的差距很大。
最初代码如下,直接执行sql语句,外加事务提升性能:- SQLiteDatabase database = new SQLiteDatabase();
- if (database.isOpen())
- {
- database.beginTransaction();
- try {
- //sql为insert into tableName (name) values ("test")
- database.execSQL(sql);
- }
- database.setTransactionSuccessful();
- } finally {
- database.endTransaction();
- }
- database.close();
- }
优化如下:
- SQLiteDatabase database = new SQLiteDatabase();
- //sql为insert into tableName (name) values (?)
- SQLiteStatement sqlListStatment = database.compileStatement(sql);
- if (database.isOpen())
- {
- database.beginTransaction();
- try {
- //index 为1开始索引,value为入库的值
- //bingXXX为插入XXX类型
- sqLiteStatement.bindString(index, value);
- sqLiteStatement.executeInsert();
- }
- database.setTransactionSuccessful();
- } finally {
- database.endTransaction();
- }
- database.close();
- }
转自
http://blog.csdn.net/efeics/article/details/18995433