在 sqlite3 中批量插入的更好方法C++

Better way to bulk insert in sqlite3 C++

本文关键字:更好 方法 C++ 插入 sqlite3      更新时间:2023-10-16

我批量插入要执行的语句。我可以创建一个包含所有插入语句的 sql 字符串并执行一次,例如,

std::string sql_string = "";
int i;
for(i=0;i<1000; i++) {
Transaction transaction = transactions[i];
std::string tx_sql = CREATE_SQL_STRING(transaction, lastTxId); // "INSERT INTO (...) VALUES (...);"
sql_string = sql_string+tx_sql;
lastTxId++;
}
sqlite3_exec(db, sql_string.c_str(), callback, 0, &zErrMsg);

有没有其他性能更好的方法来使用 sqlite3 c++ API 进行批量插入?

插入对安全并不重要。只考虑性能。我应该使用预准备语句吗?

最重要的是:将所有插入放入单个事务中。

有关性能的更多详细信息,请参阅官方文档:https://sqlite.org/speed.html(有点过时(

有关最新信息,我会重新评论 https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2

至于准备好的陈述:我不确定性能,但始终使用它们是一个很好的做法。