繁体   English   中英

为什么python插入数据库时​​比c++快?

[英]why is python faster than c ++ when inserting into a database?

我有一个由 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+"')");

基本上我的问题是是否有一种方法可以像在 Python 中一样快地在 C++ 中使用insert

这是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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM