2012-03-27 38 views
2

下面是代碼,目前我正在使用,但我正在尋找一種方法來加速我的插入,並且第一次插入100行以上...任何想法?Android:使用SQLite插入批量數據(一次)

public class SQLiteAdapter { 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 
private Context context; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 

    public long insert(String empId, String name......) 
    { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_EMP_ID, empId); 
     ..................... 
     ..................... 
     ..................... 
     return sqLiteDatabase.insert(KEY_TABLE, null, contentValues); 
    } 


    public void InsertReciters() 
    { 
    //currently, this is how i am doing, how can i speed up inserting the rows in to db? 
    //i have 100+ rows .......... 

    this.insert("ac123", ................); 
    this.insert("ac133", ................); 
    this.insert("ac143", ................); 
    this.insert("ac153", ................); 
    this.insert("ac163", ................); 
    ................. 
    ................. 
    } 
} 
+1

您可以使用預填充數據庫:http://stackoverflow.com/a/9109728/265167 – 2012-03-27 06:44:45

+0

謝謝阿古柏1+ – 2012-03-27 16:19:38

+0

準備好的發言和交易:HTTP://計算器。 com/a/29797229/3681880 – Suragch 2015-04-27 08:15:31

回答

2

建議您通過事務插入大量數據。

見下代碼格式:

List<String[]> hugeData=new ArrayList<String[]>(); 
try{ 
    sqLiteDatabase.beginTransaction(); 
    //insert huge data 
    //get pre-compiled SQLiteStatement object 
    SQLiteStatement statement=sqLiteDatabase.compileStatement("insert into tablename(..) value (?,?)") 
    for(String[] row:hugeData){ 
     statement.bindString(1,row[0]); 
     //...... 
     statement.execute(); 
    } 
    sqLiteDatabase.setTransactionSuccessful(); 
}finally{ 
    sqLiteDatabase..endTransaction(); 
} 

---------------------編輯------------ ------

public class SQLiteAdapter { 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 
private Context context; 
List<String[]> insertData=null; 

public SQLiteAdapter(Context c){ 
    context = c; 
} 
public SQLiteAdapter(Context c,List<String[]> _insertData){ 
    this(c); 
    insertData=_insertData; 
} 
    public long insert(String empId, String name......) 
    { 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_EMP_ID, empId); 
     ..................... 
     ..................... 
     ..................... 
     return sqLiteDatabase.insert(KEY_TABLE, null, contentValues); 
    } 


    public void InsertReciters() 
    { 
    //currently, this is how i am doing, how can i speed up inserting the rows in to db? 
    //i have 100+ rows .......... 

    this.insert("ac123", ................); 
    this.insert("ac133", ................); 
    this.insert("ac143", ................); 
    this.insert("ac153", ................); 
    this.insert("ac163", ................); 
    ................. 
    ................. 
    } 

    public void InsertBatchData() 
    { 
    try{ 
    sqLiteDatabase.beginTransaction(); 
    //insert huge data 
    //get pre-compiled SQLiteStatement object 
    SQLiteStatement statement=sqLiteDatabase.compileStatement("insert into tablename(..) value (?,?)") 
    for(String[] row:hugeData){ 
     statement.bindString(1,row[0]); 
     //...... 
     statement.execute(); 
    } 
    sqLiteDatabase.setTransactionSuccessful(); 
    }finally{ 
    sqLiteDatabase..endTransaction(); 
    } 
    } 
} 
+0

我不明白'.... compileStatement(「insert ....'我有100多行... – 2012-03-27 03:30:56

+0

返回預編譯的SQLi teStatement對象。然後可以使用此對象多次高效地執行此語句。 – boiledwater 2012-03-27 03:37:38

+1

好吧,所以我很困惑'價值'你想讓我定義什麼輸入行的值,然後在for循環語句?你可以使用我的代碼來演示......可能我對你使用插入語句的方式感到困惑。 – 2012-03-27 03:52:18