How to bulk insert into SQLITE database?

前端 未结 3 700
悲哀的现实
悲哀的现实 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 07:03

    You should insert all 20K records in single sqllite transaction.

    Something like this:

        public static void AddOrUpdateTickRecords( ObservableCollection tickRecords )
            {
            // Create a new connection
            using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
                {
                db.BeginTransaction();
                try
                    {
                    foreach ( var tickRecord in tickRecords )
                        {
                        if ( tickRecord.Id == 0 )
                            {
                            // New
                            db.Insert( tickRecord );
                            }
                        else
                            {
                            // Update
                            db.Update( tickRecord );
                            }
                        }
    
                    db.Commit();
                    }
                catch ( Exception )
                    {
                    db.Rollback();
                    }
                }
            }
    

提交回复
热议问题