Original path: http://blog.csdn.net/w250shini11/article/details/8649289
Recently, a customer has reported that a hundred thousand of of data needs to be imported to SQLite, and the results are lengthy and prone to crashes. Online search for the solution, see the following this feeling of the idea, but there is no corresponding large amount of data to test, the method is retained.
In Android development, you need to import a large amount of data to SQLite, according to the general practice is very time-consuming, test, import a data at around 100ms, according to this practice, if the import of 10,000 data, it takes about 17 minutes, after the actual test, it took 17 minutes or so.
The time-consuming solution is to work with SQLite's transactions. Here's how:
First get the database object, and then do the following:
Db.begintransaction (); Set up a start transaction manually
Data insertion Operation cycle
Update Insert Delete Select to loop nested operations
Db.settransactionsuccessful (); Set transaction success, do not set automatically rollback does not commit
Db.endtransaction (); Processing complete
after testing, if you import 10,000 data, it takes about 20 seconds or so. The performance ratio has been increased by a large section. like
public int Insert (URI uri, contentvalues[] values) {
Sqlitedatabase db = Mopenhelper.getwritabledatabase ();
Db.begintransaction ();
try {
int numvalues = Values.length;
for (int i = 0; i < numvalues; i++) {
Db.insertorthrow (tablename, null, values);
}
Db.settransactionsuccessful ();
} finally {
Db.endtransaction ();
}
return values.length;
}
Android Transaction Operations