python - 为什么插入数据库时python比c ++快?
问题描述
我有一个包含 100 个元素的数组,称为“项目”
Python(大约需要 1 秒):
for item in items:
cur.execute("INSERT INTO list VALUES (?)",(item,))
db.commit()
C++(9 秒):
for ( auto it = items.begin(); it != items.end(); ++it ) {
qry.prepare("INSERT INTO list VALUES (?)");
std::string item = *it;
qry.addBindValue(item);
qry.exec();
C++ 没有prepare
(9 秒):
for ( auto it = items.begin(); it != items.end(); ++it ) {
std::string item = *it;
qry.exec("INSERT INTO list VALUES ('"+item+"')");
基本上我的问题是是否有一种方法可以insert
像在 Python 中一样快速地在 C++ 中使用。
解决方案
这是在 C++ 中快速批量插入的正确方法
花费不到 1 秒:
db.transaction();
for ( auto it = items.begin(); it != items.end(); ++it ) {
std::string item = *it;
qry.exec("INSERT INTO list VALUES ('"+item+"')");
}
db.commit();
推荐阅读
- plasticscm - 在 Plastic SCM 中,是否有合并 Oracle Forms 二进制文件的选项或工具?
- swift - MapKit 聚类在开始时没有正确聚类,它在平移后聚类
- azure - 如何升级 Helm Chart
- flutter - 有没有一种方便的方法可以在小部件构造函数中将公共字段转换为私有字段?[安卓工作室]
- php - PHP脚本不断将相同的字符串写入文件
- antlr - ANTLR4 语法数学运算符关联性和运算顺序
- python - PolynomialFeatures 的迭代问题
- ios - Fastlane 无法上传 dSYM 文件
- apache-spark - 如何使用变量过滤数据集行
- java - 在 Eclipse Scala IDE 的 C:\\Program Files\java 中找不到 JRE