How to store data offline and then update it in a SQLite database in batch mode?

I am currently trying to populate a SQLite database with tens of thousands of text data using this method:

SQLiteConnection = new SQLiteConnection(cntnStr);
connection.Open();

foreach(Page p in pages)
{
     using (SQLiteCommand command = new SQLiteCommand(String.Format("Insert Into Pages (ID, Data, Name) Values ({0}, '{1}', '{2}')", id, p.Data, p.Name), connection))
         command.ExecuteNonQuery();
}

However, I suspect that this is approximately 10 times per second, probably slowing down the whole process. Is there a way that I can collect data in memory and then add every 5000 records or so to the database in batch mode (so is it faster)?

EDIT: Super-important: make sure you run all your SQL commands in DbTransaction- in this case SQLiteTransaction:

SQLiteTransaction trans = connection.BeginTransaction();

// SQL centric code - repeated inserts/changes

trans.Commit(); // adds your changes

This improves performance by 1000x.

+3
source share
3

:

using (SQLiteConnection = new SQLiteConnection(cntnStr))
{
    connection.Open();

    string query = "Insert Into Pages (ID, Data, Name) Values (?, ?, ?)";
    using (SQLiteCommand command = new SQLiteCommand(query, connection)
    {
        command.Parameters.Add("id", DbType.Int32);
        command.Parameters.Add("data", DbType.String);
        command.Parameters.Add("name", DbType.String);
        foreach(Page p in pages)
        {
             command.Parameters["id"].Value = p.Id;
             command.Parameters["data"].Value = p.Data;
             command.Parameters["name"].Value = p.Name;
             command.ExecuteNonQuery();
        }
    }
}

, DbCommand , . , SQL -

BTW, ( SQLite.NET)

+6

, .

IE:

Insert Into Pages (ID, Data, Name) Values (...),(...),(...),(...)
0

SQLite DataTable -Object, DataTable-Object 5000 .

0

Source: https://habr.com/ru/post/1715773/


All Articles