How we can insert multiple rows in Sqlite database at same time? I'm using Android with Java.
4 Answers
1 Comment
Amir Ismail
InsertHelper is deprecated now.What you are looking for is bulkInsert. It will allow you to supply an array of ContentValues to insert. See the Android docs for more info:
1 Comment
Leonardo Costa
It is important to note that the documentation says: "This function make no guarantees about the atomicity of the insertions." But I could't find a better way to insert multiple rows.
ContentValues values = new ContentValues();
for(int i = 0; i<=5; i++) {
values.put(COLUMN_NAME, i);
values.put(COLUMN_NAME, 0);
db.insert(TABLE_NAME, null, values);
}
1 Comment
Stefan Alexandru
is this efficient? can't you write all the rows with one request?
Very old post however. When you are trying to do multiple calls to the db such as update or insert, wrap around with transactions. This will make a huge difference in speed.
db.beginTransaction();
try
{
while(...)
{
// .. do your inserts, updates and deletes here
}
// If successful commit those changes
db.setTransactionSuccessful();
}
finally
{
db.endTransaction();
}
1 Comment
SuperHaker
Can you explain more on how does this affect?