加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Sqlite3插入大量数据性能优化

发布时间:2020-12-12 20:00:39 所属栏目:百科 来源:网络整理
导读:最近做的一个项目数据量非常大,文本数据有30多M。这样就遇到一个问题。插入数据库时非常慢。 这里记录下,优化方法非常简单。 原文地址:http://blog.csdn.net/qqmcy/article/details/32173681 在数据库的sql语句前加:"begin;n" 结束后加“commit;n”;

最近做的一个项目数据量非常大,文本数据有30多M。这样就遇到一个问题。插入数据库时非常慢。

这里记录下,优化方法非常简单。

原文地址:http://blog.csdn.net/qqmcy/article/details/32173681


在数据库的sql语句前加:"begin;n" 结束后加“commit;n”;


         string strSql;
        strSql += "begin;n";
        for (unsigned int i = 0 ; i < v.Size(); ++i)
        {
            //unsigned int i = 0;
            
            const rapidjson::Value &val = v[i];
            
            auto month = new MothData();
            month->initWithDictionary(val);
           
            strSql += StringUtils::format("INSERT INTO Car_mqpfl(father_code,organ_code,organ_name,report_name,stat_month,mqpfl_dn,mqpfl_qn,mqpfl_sn,xh) VALUES ('%s','%s','%d');n",month->father_code.c_str(),month->organ_code.c_str(),month->organ_name.c_str(),month->report_name.c_str(),month->stat_month.c_str(),month->mqpfl_dn.c_str(),month->mqpfl_qn.c_str(),month->mqpfl_sn.c_str(),month->xh);
        }
         strSql+= "commit;n";
        SqlDB::insertData(strSql);

void SqlDB::insertData(std::string sql)
{
    result = sqlite3_exec(_db,sql.c_str(),nullptr,&errMsg);
    
    
    if (result != SQLITE_OK) {
        log("insert error,code = %d,message = %sn",result,errMsg);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读