Android Transaction Operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.