c# - How to bulk insert into SQLITE database? -


i developing uwp application.

i have database should initialized 20,000 records. records, defined follows:

private static readonly observablecollection<tickrecord> tickrecords = new observablecollection<tickrecord>(); 

i tried insert records 1 @ time 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 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, way slow.

i modify can "bulk insert".

how do that?

thx

try insertall , updateall functions. opens database table once , inserts/updates @ once. need figure out objects insert/update ahead of time, should still speed things up.

list<tickrecords> updates = new list<tickrecords>();   list<tickrecords> inserts = new list<tickrecords>();    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); } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -