0

I have very large table 300,000 records I am loading from XML file to SQLite,

unfortunately, this is taking very very long time (took 15 min then I had to stop it).

is this normal?

is there anything wrong with my code.

how to speed it up?

thanks

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_filter);

        new Thread(new Runnable() {
            public void run() {
                FillLocations();
            }
        }).start();

}




private void FillLocations()
{
    OpenDB();
    Resources res = getResources();
    ContentValues _Values = new ContentValues();

    //Open xml file
    XmlResourceParser _xml = res.getXml(R.xml.locations);
    try
    {
        //Check for end of document
        int eventType = _xml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            //Search for record tags
            if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("Cities"))){
                //Record tag found, now get values and insert record
                String City = _xml.getAttributeValue(null, "City");
                String Country = _xml.getAttributeValue(null, "Country");
                String Time = _xml.getAttributeValue(null, "Time");
                _Values.put("City", City);
                _Values.put("Country", Country );
                _Values.put("Time", Time);
                db.insert("Locations", null, _Values);
            }
            eventType = _xml.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
6
  • Have you benchmarked your code to see exactly where it spends that much time? Also, why not pre-populate sqllite and distribute it as a binarly blob? Commented Jul 10, 2014 at 6:59
  • How can I pre-populate sqllite and distribute it as a binarly blob? Commented Jul 10, 2014 at 7:02
  • 1
    @asmgx you can try with this github.com/jgilfelt/android-sqlite-asset-helper OR you can copy the file manually Commented Jul 10, 2014 at 7:07
  • you mean to copy the SQLite file manually? is this possible? how to it? Commented Jul 10, 2014 at 7:17
  • If you don't know how to copy it manually use the library in the link above. It will copy the database file for you. Commented Jul 10, 2014 at 7:51

1 Answer 1

1

Try this code:

Before while loop beginTransaction and after successful insertion of all rows endSuccessfullTransaction.

public static void beginTransaction()
{
    final SQLiteDatabase db = openDatabase();
    if (db != null)
    {
        db.beginTransaction();
    }
}

public static void endSuccessfullTransaction()
{
    final SQLiteDatabase db = openDatabase();
    db.setTransactionSuccessful();
    db.endTransaction();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.