mysql查询后调用mysql_free_result_怎么释放_关于mysql_free_result和mysql_close的解惑
之前用mysql的時(shí)候一直是在用短鏈接,調(diào)用mysql_store_result獲取一次數(shù)據(jù)之后就直接調(diào)用:
以下是代碼片段: mysql_free_result(m_result); mysql_close(m_Database);
但是有兩個(gè)問題:
以下是引用片段:
1.當(dāng)使用長連接時(shí)(即connect之后一直不close),如果最后會(huì)調(diào)用mysql_close,需不需要每次都調(diào)用mysql_free_result呢?
2.當(dāng)mysql_close調(diào)用之后,m_result的數(shù)據(jù)是否還可以用。
先說一下結(jié)論:
1.必須每次調(diào)用。因?yàn)榻?jīng)過測試,每次mysql_store_result的指針都是不同的,可見并不是共享了同一塊buf。
2.還是可以使用。經(jīng)過valgrind掃描,只調(diào)用mysql_close的掃描結(jié)果是:
以下是引用片段: ==9397==?16,468?(88?direct,?16,380?indirect)?bytes?in?1?blocks?are?definitely?lost?in?loss?record?4?of?5 ==9397==????at?0x40219B3:?malloc?(vg_replace_malloc.c:195) ==9397==????by?0x8053EA2:?my_malloc?(in?/data/home/dantezhu/appbase/application/platform/openqqcom/share/db_openright/test/test) ==9397==????by?0x806D314:?mysql_store_result?(in?/data/home/dantezhu/appbase/application/platform/openqqcom/share/db_openright/test/test) ==9397==????by?0x804BB04:?CMySQLCppClient::Result(st_mysql_res*&)?(mysql_cpp_client.cpp:127) ==9397==????by?0x804AB58:?CDBOpenRight::GetUinsByApp(unsigned?int,?std::set,?std::allocator?>&)?(db_openright.cpp:58) ==9397==????by?0x8049F10:?main?(test.cpp:27)
這里連同測試代碼和我之前寫的一個(gè)簡單的C++封裝的mysql庫一起放出下載,有需要的同學(xué)可以下載試試:
代碼下載
其中只有mysql_cpp_client.h和mysql_cpp_client.cpp是核心文件,其他均為測試代碼.
里面有簡單的演示,如查詢:
char strSql[MAX_QUERYLEN_OPENRIGHT];
snprintf(strSql,sizeof(strSql),"select uin \ from %s where appid=%u;",OPENRIGHT_TB_CARE,appid);
int ret;
ret = m_SqlClient.Execute(strSql);
if(ret)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error:[%d][%s]\n",
__FILE__,__LINE__,__FUNCTION__,ret,m_SqlClient.GetErrMsg());
return -1;
}
MYSQL_RES *result = NULL;
ret = m_SqlClient.Result(result);
if(ret)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error:[%d][%s]\n",
__FILE__,__LINE__,__FUNCTION__,ret,m_SqlClient.GetErrMsg());
return -2;
}
//這里很重要,做了析構(gòu)時(shí)自動(dòng)調(diào)用mysql_free_result
StCppResult freeRes(result);
unsigned int unRecords = mysql_num_rows(result);
if (0 == unRecords)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error: Result is empty\n",
__FILE__,__LINE__,__FUNCTION__);
return 0;
}
MYSQL_ROW row;
for(unsigned int unIndex = 0; unIndex < unRecords; unIndex++)
{
row=mysql_fetch_row(result);
unsigned uin = unsigned(atoi((char*)row[0]));
setUins.insert(uin);
}
return 0;
插入:
if(setUins.size() <= 0)
{
return 0;
}
if(setUins.size() > 1000)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error:[uins more than 1000]\n",
__FILE__,__LINE__,__FUNCTION__);
return -1;
}
string strValues;
char szValue[100];
for(set::iterator it = setUins.begin();it!=setUins.end();++it)
{
if (setUins.begin() == it)
{
snprintf(szValue,sizeof(szValue),TPL_ADDUIN2APP,*it,appid);
}
else
{
snprintf(szValue,sizeof(szValue),","TPL_ADDUIN2APP,*it,appid);
}
strValues.append(szValue);
}
char strSql[MAX_QUERYLEN_OPENRIGHT];
snprintf(strSql,sizeof(strSql),"insert into %s(uin,appid) VALUES %s;",OPENRIGHT_TB_CARE,strValues.c_str());
int ret;
ret = m_SqlClient.Execute(strSql);
if(ret)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error:[%d][%s]\n",
__FILE__,__LINE__,__FUNCTION__,ret,m_SqlClient.GetErrMsg());
return -2;
}
if(m_SqlClient.AffectRows()<=0)
{
snprintf(m_StrErrMsg,sizeof(m_StrErrMsg),"[%s][%d][%s]Error:[%d][%s]\n",
__FILE__,__LINE__,__FUNCTION__,ret,m_SqlClient.GetErrMsg());
return -3;
}
return 0;
OK,就這樣。
FROM: http://blogread.cn/it/article/2495
總結(jié)
以上是生活随笔為你收集整理的mysql查询后调用mysql_free_result_怎么释放_关于mysql_free_result和mysql_close的解惑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打了断点为直接运行完_BBC主持人多次打
- 下一篇: mysql有没有类似merge_有关于M