0

我有要执行的批量插入语句。我可以使用所有插入语句创建一个 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 批量插入?

插入不是安全关键。只考虑性能。我应该使用准备好的语句吗?

4

1 回答 1

2

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

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

有关更多最新信息,我会推荐https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2

至于准备好的语句:我不确定性能,但始终使用它们是一个好习惯。

于 2018-06-21T17:38:11.910 回答