Android: insert batch data (one time) using SQLite
below is the code I am currently using but I am looking for a way to speed up my insert and I have 100+ rows inserted the first time ... any thoughts?
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", ................);
.................
.................
}
}
+3
source to share
1 answer
Offer you to insert huge transaction data.
See below code format:
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();
}
--------------------- edited ------------------
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();
}
}
}
+2
source to share