To insert large amount of data in android SQLite Database

孤者浪人 提交于 2019-12-11 19:39:30

问题


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.


回答1:


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();



回答2:


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();



回答3:


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(); 
        }
    }
//...
}


来源:https://stackoverflow.com/questions/18545955/to-insert-large-amount-of-data-in-android-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!