Example of SQLite BULK INSERT large data

Source: Internet
Author: User
Tags bulk insert sqlite

These two days by the database into the batch data tortured a little crazy okay found a solution, not much to say, directly look at the following two parts of the code:

Sqlitedatabase db = Dbhelper.getwritabledatabase ();
LOGUTILS.I ("Start parsing *****************" + new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
for (int i = 0; i < messagearray.length (); i++) {
try {
Jsonobject Messageobject = (jsonobject) messagearray.get (i);
int stamp = Jsonutils.getint (Messageobject, "Ntime", 0);
String Dblon = jsonutils.getstring (Messageobject, "Dblon", "");
String Dblat = jsonutils.getstring (Messageobject, "Dblat", "");
/******************** Database Transactions Start **************************/
Db.begintransaction ();
Db.execsql ("INSERT into Waybaby (stamp,lontude,lantude) VALUES (?,?,?)", new object[] {stamp, dblon,dblat});
Db.settransactionsuccessful ();
Db.endtransaction ();
/******************** database Transaction End **************************/
Trancelist.add (Double.parsedouble (Dblat));
Trancelist.add (Double.parsedouble (Dblon));
LATLNG latlng = new Latlng (double.parsedouble (Dblat), double.parsedouble (Dblon));
Latlngline.add (LATLNG);
catch (Jsonexception e) {
E.printstacktrace ();
}
}
Dismissprogressdialog ();
LOGUTILS.I ("Parsing end *****************" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
Draw the trajectory of the course
Onmaploaded ();

This code is used to parse the data, and then insert into the local database, total: 1000 data, When: 10 seconds ~~15 seconds. 15 seconds of waiting, the flowers are all thanks. Take a look at the modified code below:


Sqlitedatabase db = Dbhelper.getwritabledatabase ();
LOGUTILS.I ("Start parsing *****************" + new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
for (int i = 0; i < messagearray.length (); i++) {
try {
Jsonobject Messageobject = (jsonobject) messagearray.get (i);
int stamp = Jsonutils.getint (Messageobject, "Ntime", 0);
String Dblon = jsonutils.getstring (Messageobject, "Dblon", "");
String Dblat = jsonutils.getstring (Messageobject, "Dblat", "");
Locusentity locusentity = new Locusentity (stamp, Dblon, Dblat);
Locusentities.add (locusentity);
Trancelist.add (Double.parsedouble (Dblat));
Trancelist.add (Double.parsedouble (Dblon));
LATLNG latlng = new Latlng (double.parsedouble (Dblat), double.parsedouble (Dblon));
Latlngline.add (LATLNG);
catch (Jsonexception e) {
E.printstacktrace ();
}
}
LOGUTILS.I ("Start inserting data *****************" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
Db.begintransaction ();
String sql = "INSERT into Waybaby (stamp,lontude,lantude) VALUES (?,?,?)";
for (locusentity locusentity:locusentities) {
Sqlitestatement stat = db.compilestatement (SQL);
Stat.bindlong (1, Locusentity.getstamp ());
Stat.bindstring (2, Locusentity.getlontude ());
Stat.bindstring (3, Locusentity.getlantude ());
Stat.executeinsert ();
}
Db.settransactionsuccessful ();
Db.endtransaction ();
Db.close ();
LOGUTILS.I ("Insert data complete *****************" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
Dismissprogressdialog ();
LOGUTILS.I ("Parsing end *****************" + New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date ());
Draw the trajectory of the course
Onmaploaded ();

Careful of you, you will find that the operation of inserting data, was taken to the outside of the For loop, while the operation of inserting the database also changed, a large number of code, from a row into more than 10 lines, but the efficiency of the improvement is not a little two points. This code executes time: Less than 1 seconds, print time information, you will find that time does not take 1 seconds to obtain success. I've been printing for less than 1 seconds. Nearly 10 times times faster than before, you dare believe? This is the truth. As to what that piece of code means. Please own Baidu, learn by yourself.

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.