0

I am new in Android Development.

Currently, working with SQLite Database in Android.

My problem is that I have a large amount of data which I have to store in SQLite Database in Android.

There are 2 tables: One having 14927 rows and the other one has 9903 rows.

Currently the database in sql. And I have copy these data in excel sheet but don't understand how can I import these data in SQLite Database.

I go through the following link:

Inserting large amount of data into android sqlite database?

Here, the solution is posted regarding CSV File. But want to know other ways of doing this.

Please let me know what is the best way to import such a large data in Android.

Please help me. Thanks in advance.

Community
  • 1
  • 1
Manali Sheth
  • 379
  • 2
  • 5
  • 17

3 Answers3

2

Do Like This

SQLiteDatabase sd;
    sd.beginTransaction();


        for (int i = 0; i < data.size(); i++) {
            ContentValues values = new ContentValues();
            values.put(DBAdapter.Column1,  "HP");
            values.put(DBAdapter.Column2,  "qw");
            values.put(DBAdapter.Column3,  "5280");
            values.put(DBAdapter.Column4,  "345, 546");
            sd.insert(DBAdapter.TABLE, null, values);
            sd.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_IGNORE);

        }

    sd.setTransactionSuccessful();
    sd.endTransaction();
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

Try this

 SQLiteDatabase db = Your_DATABASE;
 db.beginTransaction();
 db.openDatabase(); 

for (int i = 0; i < array.size(); i++) 
{
    String sql = ( "INSERT INTO " + Table_NAME 
                                  + "(" + COLUMN_1 + "," 
                                        + COLUMN_2 + ","  
                                        + COLUMN_3 + "," 
                                        + COLUMN_4 + "," 
                                  + ") values (?,?,?,?)");

    SQLiteStatement insert = db.compileStatement(sql);
}

db.setTransactionSuccessful();
db.endTransaction();
db.closeDatabase();
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
0

Use DatabaseUtils.InsertHelper. In this article you will find an example of how to use it and other ways to speed up insertions. The example is as follows:

import android.database.DatabaseUtils.InsertHelper;
//...
private class DatabaseHelper extends SQLiteOpenHelper {
    @Override
    public void onCreate(SQLiteDatabase db) {
        // Create a single InsertHelper to handle this set of insertions.
        InsertHelper ih = new InsertHelper(db, "TableName");

        // Get the numeric indexes for each of the columns that we're updating
        final int greekColumn = ih.getColumnIndex("Greek");
        final int ionicColumn = ih.getColumnIndex("Ionic");
        //...
        final int romanColumn = ih.getColumnIndex("Roman");

        try {
            while (moreRowsToInsert) {
                // ... Create the data for this row (not shown) ...

                // Get the InsertHelper ready to insert a single row
                ih.prepareForInsert();

                // Add the data for each column
                ih.bind(greekColumn, greekData);
                ih.bind(ionicColumn, ionicData);
                //...
                ih.bind(romanColumn, romanData);

                // Insert the row into the database.
                ih.execute();
            }
        }
        finally {
            ih.close(); 
        }
    }
//...
}
Jaime Marín
  • 578
  • 6
  • 11