Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:59:55+00:00 2026-06-04T11:59:55+00:00

I currently have a CSV file that I parse and am trying to insert

  • 0

I currently have a CSV file that I parse and am trying to insert the data into the android database. The problem I am having is that it is taking way too long to insert all of the data. It’s a good amount of data but I feel like it shouldn’t take 20min or so to complete.

Basically, I create my database, then begin the parsing. While parsing through each individual CSV row, I grab the required data and insert it into the database. In total there are around 40000 rows.

Is there any way I can speed up this process? I have tried batch inserts but it never really helped (unless I did it wrong).

Code down below.

Thanks.

DatabaseHelper (i have two insert commands based on the amount of data in each csv row):

// add zipcode
    public void add9Zipcode(String zip, String city, String state, String lat,
            String longi, String decom) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{

            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        db.close();

    }

    public void add12Zipcode(String zip, String city, String state, String lat,
            String longi, String decom, String tax, String pop, String wages) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{
            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);
            values.put(KEY_TAX, tax);
            values.put(KEY_POP, pop);
            values.put(KEY_WAGES, wages);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally{
            db.endTransaction();  
        }


        db.close();
}

Parse File:

public void parse(ArrayList<String> theArray, DatabaseHandler db) {

        String[] data = null;

        // while loop to get split the data into new lines
        // for loop to split each string in the array list of zipcodes
        for (int x = 0; x < theArray.size(); x++) {

            if(x == 10000 || x == 20000 || x == 30000 || x == 40000){
                Log.d(TAG, "x is 10k, 20k, 30k, 40k");
            }

            // split string first into an array
            data = theArray.get(x).split(",");

            // separate based on the size of the array: 9 or 12
            if (data.length == 9) {

                db.add9Zipcode(data[0], data[2], data[3], data[5], data[6],
                        data[8]);

            } else if (data.length == 12) {

                db.add12Zipcode(data[0], data[2], data[3], data[5], data[6],
                        data[8], data[9], data[10], data[11]);

                /*
                 * theZip.zip = data[0]; theZip.city = data[2]; theZip.state =
                 * data[3]; theZip.lat = data[5]; theZip.longi = data[6];
                 * theZip.decom = data[8]; theZip. = data[9]; theZip.population
                 * = data[10]; theZip.wages = data[11];
                 */

            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:59:56+00:00Added an answer on June 4, 2026 at 11:59 am

    Refer to this answer I made previously: Inserting 1000000 rows in sqlite3 database

    In short, use an InsertHelper and do more than one insert per transaction – unless you did something wonky, the speed increase should be noticeable.

    Edit:
    In short:

    1. Your SQLiteOpenHelper should be a singleton used across your entire application.
    2. Don’t go around calling close() on your SQLiteDatabase instance – it’s cached in the SQLiteOpenHelper and every time you close you force the helper to reopen it.
    3. Batch your inserts, start a transaction outside the call to the addZipCode methods and mark it as successful after you’ve done all the inserts – then commit the transaction.
    4. Use an InsertHelper – it will format the insert properly as a prepared statement and is nice and reusable.
    5. Be mindful of synchronizing access to the database – unless you intend to do all your database work on the UI-thread (which is not recommended) – you either need to enable locking or guard access to the database to avoid concurrent access.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a sproc that is exporting data to a csv file using
I currently have a csv file that I'm parsing with an example from here:
I currently have a .csv file with several unlabeled columns of data, which to
I'm currently just starting my first Lua program and I have a .csv file
So I have a CSV file that looks like this: 12345, Here is some
I have a jpeg file that is being held as a list(of Byte) Currently
I have a stack of CSV files I want to parse - the problem
I have a file that is really in csv format but is being handed
I'm trying to read a CSV file with a date format 06/01/05 (month/date/year) into
I'm currently developing an application that needs to parse some (a big file of)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.