How to bulk insert into SQLITE database?

前端 未结 3 678
悲哀的现实
悲哀的现实 2021-01-15 06:37

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:

p         


        
3条回答
  •  借酒劲吻你
    2021-01-15 06:48

    Try the InsertAll and UpdateAll functions. Hopefully this opens up the database table just once and inserts/updates everything at once. You will need to figure out which objects to insert/update ahead of time, but this should still really speed things up.

    List updates = new List(); 
    
    List inserts = new List();  
    
    foreach ( var tickRecord in tickRecords ) 
    {   
        if ( tickRecord.Id == 0 )
        {       
            updates.Add(tickRecord);
        }
        else
        {       
            inserts.Add(tickRecords);
         } 
    }
    
    
    
    using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) ) 
    {
         db.InsertAll(inserts);
         db.UpdateAll(updates);
    }
    

提交回复
热议问题