SQLite Insert Batch Data optimization

Source: Internet
Author: User

Inserting SQLite into the data method

1,execsql () Directly splicing SQL statements

2,insert ()

3,compilestatement () pretreatment

The three-way insertion data is relatively fast, in turn compilestatement, Insert,execsql

Inserting large amounts of data is combined with transactions

1  PackageCom.example.natedb;2 3 ImportAndroid.content.Context;4 Importandroid.database.sqlite.SQLiteDatabase;5 Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;6 ImportAndroid.database.sqlite.SQLiteOpenHelper;7 8  Public classSqlDbextendsSqliteopenhelper {9 Ten      PublicSqlDb (Context context) { One          This(Context, "lihao.db",NULL, 1); A     } -      PublicSqlDb (Context context, String name, Cursorfactory factory, -             intversion) { the         Super(context, name, Factory, version); -          -     } -  + @Override -      Public voidonCreate (Sqlitedatabase db) { +         //TODO auto-generated Method Stub ADb.execsql ("CREATE TABLE person (_id integer PRIMARY key autoincrement,name text,age text)"); at     } -  - @Override -      Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { -         //TODO auto-generated Method Stub -  in     } -  to      +}

Time used to insert 1000 data using Execsql ()

    New SqlDb (mainactivity.  this);      = db.getwritabledatabase ();   for (int i = 0; i < i++) {        database.execsql ("Insert to Person (name,ag  e) VALUES (' Lihao ', ' n ') '); }

Insertion time: 59504

Use Insert ()

1SqlDb db =NewSqlDb (mainactivity. This);2Sqlitedatabase Database =db.getwritabledatabase ();3  for(inti = 0; i < 1000; i++) {4Contentvalues values =Newcontentvalues ();5Values.put ("name", "Lihao");6Values.put ("Age", "12");7Database.insert ("Person",NULL, values); 8}

Insertion time: 57444

Using Compilestatement preprocessing

1SqlDb db =NewSqlDb (mainactivity. This);2Sqlitedatabase Database =db.getwritabledatabase ();3Sqlitestatement statement = database.compilestatement ("INSERT into person (name,age) VALUES (?,?)");4  for(inti = 0; i < 1000; i++) {5Statement.bindstring (1, "Lihao");6Statement.bindstring (2, "34");7 Statement.executeinsert ();8}

Insertion time: 50069

Using things to handle

1SqlDb db =NewSqlDb (mainactivity. This);2Sqlitedatabase database = Db.getwritabledatabase ();
Sqlitestatement statement = database.compilestatement ("INSERT into person (name,age) VALUES (?,?)"); 3 database.begintransaction ();4 for(inti = 0; i < 1000; i++) {5Statement.bindstring (1, "Lihao");6Statement.bindstring (2, "34");7 Statement.executeinsert ();8 }9 database.settransactionsuccessful ();TenDatabase.endtransaction ();

Adding things after the time required for 1300 stunned! Well, it's not a magnitude.

You can consider using things when you are processing a large number of insert data later

SQLite Insert Batch Data optimization

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.