1手动处理sqlite 的事务
http://blog.csdn.net/pipisky2006/article/details/6917399
sqlite3_exec(db, “insert into name values ‘lxkxf', ‘24'; ”, 0, 0, &zErrMsg);
将会重复的打开关闭数据库文件100万次,所以速度当然会很慢。因此对于这种情况我们应该使用“事务”。
具体方法如下:在执行SQL语句之前和SQL语句执行完毕之后加上
rc = sqlite3_exec(db, "BEGIN;", 0, 0, &zErrMsg);
//执行SQL语句
rc = sqlite3_exec(db, "COMMIT;", 0, 0, &zErrMsg);
CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));
INSERT INTO t1 VALUES(1,13153,'thirteen thousand one hundred fifty three');
INSERT INTO t1 VALUES(2,75560,'seventy five thousand five hundred sixty');
... 995 lines omitted
INSERT INTO t1 VALUES(998,66289,'sixty six thousand two hundred eighty nine');
INSERT INTO t1 VALUES(999,24322,'twenty four thousand three hundred twenty two');
INSERT INTO t1 VALUES(1000,94142,'ninety four thousand one hundred forty two');
SQLite 2.7.6:
13.061
SQLite 2.7.6 (nosync):
0.223
测试2: 使用事务 25000 INSERTs
BEGIN;
CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));
INSERT INTO t2 VALUES(1,59672,'fifty nine thousand six hundred seventy two');
... 24997 lines omitted
INSERT INTO t2 VALUES(24999,89569,'eighty nine thousand five hundred sixty nine');
INSERT INTO t2 VALUES(25000,94666,'ninety four thousand six hundred sixty six');
COMMIT;
SQLite 2.7.6:
0.914
SQLite 2.7.6 (nosync):
0.757
可见使用了事务之后却是极大的提高了数据库的效率。但是我们也要注意,使用事务也是有一定的开销的,所以对于数据量很小的操作可以不必使用,以免造成而外的消耗。
解决方法:
添加事务处理,把5000条插入作为一个事务
dataBase.beginTransaction(); //手动设置开始事务
//数据插入操作循环
dataBase.setTransactionSuccessful(); //设置事务处理成功,不设置会自动回滚不提交
dataBase.endTransaction(); //处理完成
2android 自带的方法
http://disanji.net/2011/03/20/android-6-insert-update-and-delete-content/
添加、更新和删除内容
在Content Provider上执行交互,通过调用ContentResolver对象的delete、update和insert方法。
Insert
ContentResolver提供了两个插入新的记录的方法——insert和bulkInsert。两个方法都接受你想添加的项目类型的URI;前者接受单一的ContentValues对象,后者接受一个数组。
简单的insert方法会返回新添加的记录的URI,而bulkInsert会返回成功添加的项目个数。
下面的代码片段显示了如何使用insert方法和bulkInsert方法:
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put(COLUMN_NAME, newValue);
[ ... Repeat for each column ... ]
Uri myRowUri = getContentResolver().insert(MyProvider.CONTENT_URI, newValues);
// Create a new row of values to insert.
ContentValues[] valueArray = new ContentValues[5];
// TODO: Create an array of new rows
int count = getContentResolver().bulkInsert(MyProvider.CONTENT_URI, valueArray);
Delete
为了通过ContentResolver来删除一条记录,调用delete,传入你想删除的行的URI。可变通的地方,你可以指定一个where语句来删除多个行。两种技巧都在下面的片段中显示:
// Remove a specific row.
getContentResolver().delete(myRowUri, null, null);
// Remove the first five rows.
String where = “_id < 5”;
getContentResolver().delete(MyProvider.CONTENT_URI, where, null);
Update
通过在ContentResolver上调用update方法来对Content Provider进行更新。update方法需要目标Content Provider的URI,一个更新了数据的ContentValues对象,还有一个where语句来指定哪些行要被更新。
当执行时,与where语句匹配的行都会使用传入的ContentValues对象进行更新,并返回成功更新的行的数目。
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Create a replacement map, specifying which columns you want to
// update, and what values to assign to each of them.
newValues.put(COLUMN_NAME, newValue);
// Apply to the first 5 rows.
String where = “_id < 5”;
getContentResolver().update(MyProvider.CONTENT_URI, newValues, where, null);
所有评论(0)