简体   繁体   English

在android SQLite数据库中插入大量数据

[英]To insert large amount of data in android SQLite Database

I am new in Android Development. 我是Android开发的新手。

Currently, working with SQLite Database in Android. 目前,正在使用Android中的SQLite数据库。

My problem is that I have a large amount of data which I have to store in SQLite Database in Android. 我的问题是我必须将大量数据存储在Android的SQLite数据库中。

There are 2 tables: One having 14927 rows and the other one has 9903 rows. 有2个表:一个表有14927行,另一表有9903行。

Currently the database in sql. 当前数据库在sql中。 And I have copy these data in excel sheet but don't understand how can I import these data in SQLite Database. 而且我已将这些数据复制到excel工作表中,但不了解如何将这些数据导入SQLite数据库。

I go through the following link: 我通过以下链接:

Inserting large amount of data into android sqlite database? 将大量数据插入android sqlite数据库?

Here, the solution is posted regarding CSV File. 在这里,有关CSV文件的解决方案已发布。 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. 请让我知道在Android中导入如此大数据的最佳方法是什么。

Please help me. 请帮我。 Thanks in advance. 提前致谢。

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

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

Use DatabaseUtils.InsertHelper . 使用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(); 
        }
    }
//...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM