5

How we can insert multiple rows in Sqlite database at same time? I'm using Android with Java.

1
  • 1
    In which programming language? Commented Dec 10, 2010 at 7:18

4 Answers 4

5

you can use inserthelper, check this blog

Sign up to request clarification or add additional context in comments.

1 Comment

InsertHelper is deprecated now.
5

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:

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

1 Comment

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.
2
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

is this efficient? can't you write all the rows with one request?
0

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

Can you explain more on how does this affect?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.