簡體   English   中英

在android SQLite數據庫中插入大量數據

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

我是Android開發的新手。

目前,正在使用Android中的SQLite數據庫。

我的問題是我必須將大量數據存儲在Android的SQLite數據庫中。

有2個表:一個表有14927行,另一表有9903行。

當前數據庫在sql中。 而且我已將這些數據復制到excel工作表中,但不了解如何將這些數據導入SQLite數據庫。

我通過以下鏈接:

將大量數據插入android sqlite數據庫?

在這里,有關CSV文件的解決方案已發布。 但是想知道其他方法。

請讓我知道在Android中導入如此大數據的最佳方法是什么。

請幫我。 提前致謝。

喜歡這個

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

嘗試這個

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

使用DatabaseUtils.InsertHelper 本文中,您將找到有關如何使用它以及其他加快插入速度的示例。 示例如下:

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