如何离线存储大量数据,然后批量更新到SQLite数据库中?

发布于 2024-08-02 19:46:49 字数 757 浏览 3 评论 0原文

目前,我正在尝试使用此方法用数万个文本数据填充 SQLite 数据库:

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();
}

但是,我怀疑每秒执行此操作大约 10 次可能会减慢整个过程。有没有办法可以整理内存中的数据,然后将每 5000 条左右的记录批量添加到数据库中(这样更快)?

编辑:非常重要:确保在 DbTransaction 中执行所有 SQL 命令 - 在本例中为 SQLiteTransaction

SQLiteTransaction trans = connection.BeginTransaction();

// SQL centric code - repeated inserts/changes

trans.Commit(); // adds your changes

它将性能提高 1000 倍。

Currently, I am trying to fill an SQLite database with tens of thousands of text data using this 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 doing this about 10 times per second is probably slowing the whole process down. Is there a way I can collate the data in memory and then add every 5000 records or so into the database in batch (so it is faster)?

EDIT: Super-important: Make sure you perform all your SQL commands within a DbTransaction - in this case an SQLiteTransaction:

SQLiteTransaction trans = connection.BeginTransaction();

// SQL centric code - repeated inserts/changes

trans.Commit(); // adds your changes

It improves performance by 1000x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哽咽笑 2024-08-09 19:46:49

使用参数化查询,而不是使用字符串连接构建查询:

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 注入的风险

,请查看 这篇文章由 Robert Simpson(SQLite .NET 提供程序的作者)撰写

Use a parameterized query, instead of building the query using string concatenation :

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();
        }
    }
}

This will be faster because the DbCommand is only created once, and the query is only parsed once. Also, you avoid the risks of SQL injection due to the string concatenation

BTW, have a look at this article by Robert Simpson (author of the SQLite .NET provider)

肥爪爪 2024-08-09 19:46:49

您必须自己处理转义数据,但可以使用批量插入。

IE:

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

You will have to handle escaping the data yourself, but you can use a batch insert.

IE:

Insert Into Pages (ID, Data, Name) Values (...),(...),(...),(...)
何以笙箫默 2024-08-09 19:46:49

您可以将表从 SQLite DB 加载到 DataTable-Object,然后将记录插入 DataTable-Object 中,并每 5000 条记录同步回数据库。

You could load the table from the SQLite DB into a DataTable-Object, then insert your records into the DataTable-Object and sync it back every 5000 records to the DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文