Anroid sqlite Bulk Insert Data optimization method

Source: Internet
Author: User
Tags bulk insert sqlite database

SQLite database is essentially a disk file, so all the database operation will be converted to the operation of the file, and frequent file operations will be a good process, will greatly affect the speed of database access. For example, insert 1 million data into the database and, by default, if only the execution
Sqlite3_exec (db, "INSERT into name values ' lxkxf ', '"; ", 0, 0, &zerrmsg);
The database file will be opened repeatedly 1 million times, so the speed of course will be very slow. So we should use "transaction" for this situation.
Here's how to do this: before executing the SQL statement and after executing the SQL statement, add
rc = sqlite3_exec (db, "BEGIN;", 0, 0, &zerrmsg);
Execute SQL statement
rc = sqlite3_exec (db, "COMMIT;", 0, 0, &zerrmsg), so SQLite will put all the SQL statements to be executed in memory first, and then wait until COMMIT to write to the database once, In this way, the database file is only opened and closed once, the efficiency naturally greatly improved. There is a set of data comparisons: Test 1:1000 Inserts
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, Hundred Eighty Nine ');
INSERT into T1 VALUES (999,24322, ' Twenty four thousand three hundred Twenty ');
INSERT into T1 VALUES (1000,94142, ' Ninety four thousand one hundred Forty ');
SQLite 2.7.6:
13.061
SQLite 2.7.6 (nosync):
0.223
Test 2: Using transaction 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 ');
... 24997 lines omitted
INSERT into T2 VALUES (24999,89569, ' Eighty nine thousand five hundred sixty ');
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
It can be seen that the use of transactions has greatly improved the efficiency of the database. However, we also have to note that the use of transactions is also a certain cost, so for the small amount of data operations can not be used, so as not to cause the consumption of the outer.
Methods in Android,

Workaround:

Add a transaction, insert 5,000 as a transaction

Database.begintransaction (); Set up a start transaction manually

Data insertion Operation cycle

Database.settransactionsuccessful (); Set transaction success, do not set automatically rollback does not commit

Database.endtransaction (); Processing complete




PS, usually using Contentresolver to pack SQLite in Android, with a bulk insert methodPublic Final intBulkinsert(Uri URL, contentvalues[] values)SINCE:API Level 1

Inserts multiple rows into a table at the given URL. This function make no guarantees about the atomicity of the insertions.

Parameters
Url The URL of the table to insert into.
Values The initial values for the newly inserted rows. The key is the column name for the field. Passing null would create an empty row.
Returns
    • The number of newly created rows.

Anroid sqlite Bulk Insert Data optimization method

Related Article

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.