开发者

In Sqlite database, How to Insert Multiple Rows in Table at same time

How we can insert multiple rows in Sqlite database at same time? I'm using A开发者_运维技巧ndroid with Java.


you can use inserthelper, check this blog


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜