我的程序在while循环中每次执行一个update语句,进行数据更新时用mysql_real_query()函数,然后用mysql_store_result、mysql_free_result和mysql_next_result 对结果集进行释放,当我的while循环次数比较多时,发现程序会卡住,定位原因好像是由于释放结果集时占用的时间比较长,多次循环下来就出现卡住的现象,有没有人知道 在释放结果集时是不是会比较耗时间?还是别的问题导致?

解决方案 »

  1.   

    // 数据库语句执行
    int __stdcall DbExecuteSQL(MYSQL* pMysql, const AnsiString& SQL)
    {
        //调用时保证单语句执行
        if (NULL == pMysql)
        {
            return D_FUNC_PARAM_ERROR;
        }
        try
        {
            int iCharSetRetn = mysql_set_character_set(pMysql, "gbk");
            int iQueryRetn = mysql_real_query(pMysql, SQL.c_str(), SQL.Length());
            if ((D_DB_SUCCESS != iCharSetRetn) || (D_DB_SUCCESS != iQueryRetn))
            {
                return D_DB_SQL_EXEC_FAILED;
            }
            int RecordCount = 0;
            MYSQL_RES* result = NULL;
            do
            {
                result = mysql_store_result(pMysql);
                if (NULL != result)
                {
                    RecordCount += mysql_num_rows(result);
                    mysql_free_result(result);
                    result = NULL;
                }
            } while (!mysql_next_result(pMysql));
            return RecordCount;
        }
        catch (...)
        {
            return D_DB_SQL_EXEC_FAILED;
        }
    }在while 循环中调用此函数