之前用mysql的时候一直是在用短链接,调用mysql_store_result获取一次数据之后就直接调用:
以下是代码片段: mysql_free_result(m_result); mysql_close(m_Database); |
但是有两个问题:
以下是引用片段: 1.当使用长连接时(即connect之后一直不close),如果最后会调用mysql_close,需不需要每次都调用mysql_free_result呢? 2.当mysql_close调用之后,m_result的数据是否还可以用。 |
先说一下结论:
1.必须每次调用。因为经过测试,每次mysql_store_result的指针都是不同的,可见并不是共享了同一块buf。
2.还是可以使用。经过valgrind扫描,只调用mysql_close的扫描结果是:
以下是引用片段: ==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 |
里面有简单的演示,如查询:
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;
}
//这里很重要,做了析构时自动调用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<unsigned int>::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