Android:使用SQLite插入批量数据(一次)

时间:2012-03-27 03:04:08

标签: android sqlite

下面是代码,目前我正在使用,但我正在寻找一种方法来加快我的插入,我有第一次插入100多行...任何想法?

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 个答案:

答案 0 :(得分:3)

建议您按交易插入大量数据。

见下面的代码格式:

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