sqlite3 实现批量处理 sql语句,避免频繁操作数据库,从而影响性能的问题。C++语言,begin开始事务,commit提交事务处理。
int SQL_EXE(std::vector<std::string> vtExeSql)
{
if (NULL == m_pDB)
{
return -1;
}
char* pErrMsg = NULL;
int iRet = SQLITE_OK;
iRet = sqlite3_exec(m_pDB, "BEGIN", NULL, NULL, &pErrMsg);
if (SQLITE_OK != iRet)
{
if (NULL != pErrMsg)
{
sqlite3_free(pErrMsg);
}
return -1;
}
std::vector<std::string>::const_iterator iter = vtExeSql.begin();
for (; iter != vtExeSql.end(); ++iter)
{
std::string strSql = *iter;
if (!(strSql.empty()))
{
iRet = sqlite3_exec(m_pDB, strSql.c_str(), NULL, NULL, &pErrMsg);
if (SQLITE_OK != iRet)
{
continue;
}
}
}
iRet = sqlite3_exec(m_pDB, "COMMIT", NULL, NULL, &pErrMsg);
if (SQLITE_OK != iRet)
{
iRet = -1;
}
if (NULL != pErrMsg)
{
sqlite3_free(pErrMsg);
}
return iRet;
}