In a Sqlite database, how to insert multiple rows into a table at the same time

How can we insert multiple rows into a Sqlite database at once? I am using Android with Java.

+5
source share
4 answers

What you are looking for is this bulkInsert. It will allow you to provide an array ContentValuesfor insertion. See Android docs for more info:

http://developer.android.com/reference/android/content/ContentResolver.html#bulkInsert%28android.net.Uri,%20android.content.ContentValues%5B%5D%29

+5
source

you can use inserthelper check blog

+5
source
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

Very old post. When you try to make several calls in db, such as update or insert, wrap 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();
}
0
source

All Articles