jeudi 21 avril 2016

How to bulk insert into SQLITE database?

I am developing UWP application.

I have a database that should be initialized with about 20,000 records. The records, that are defined as follows:

private static readonly ObservableCollection<TickRecord> TickRecords = new ObservableCollection<TickRecord>();

I tried to insert the records one at a time like this:

private void CreateFakeTickRecords()
{
if ( Database.Database.CountTickRecords() > 0 )
    {
    return;
    }

foreach ( var tickRecord in TickRecords )
    {
    Database.Database.AddOrUpdateTickRecord( tickRecord );
    }
}

and

public static void AddOrUpdateTickRecord( TickRecord tickRecord )
{
    // Create a new connection
    using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
    {
    if ( tickRecord.Id == 0 )
        {
        // New
        db.Insert( tickRecord );
        }
    else
        {
        // Update
        db.Update( tickRecord );
        }
    }
}

This code works fine, however it is way too slow.

I would like to modify it so I can do a "Bulk Insert".

How do I do that?

Thx

Aucun commentaire:

Enregistrer un commentaire