SELECT MAX(split_part(tablename,'_',4)) AS table_date from pg_tables WHERE tablename LIKE 'tab_cdr_record_%' AND tablename NOT LIKE '%detail%' AND tablename NOT LIKE '%error%'
在数据库客户端上可以执行
但是到c++代码里就不能执行了。是不是哪边需要转义符?
求大神告知。
附上c++代码,用的pqxx库。
char szSql[128] = {0};
strcpy(szSql, "SELECT MAX(split_part(tablename,'_',4)) AS table_date from pg_tables WHERE tablename LIKE 'tab_cdr_record_%' AND tablename NOT LIKE '%detail%' AND tablename NOT LIKE '%error%'");
printf("szSql:%s\n", szSql);
Connection *pConn = CdbConncetPool::Instance()->GetConnection();
if(pConn == NULL)
{
clm_log(CLM_WARNING, "Get DbConnection Failed");
return Sdf_co_fail;
}
try
{
work wCdr(*(pConn->hDB));
pqxx::result *res = new pqxx::result(wCdr.exec(szSql));
wCdr.commit();
pqxx::result::const_iterator it = res->begin();
strcpy(szDate, it["table_date"].as<string>().data());
printf("aaaaszDate:%s\n", szDate);
delete res;
}
catch(const std::exception &e)
{
cerr << e.what() << std::endl;
CdbConncetPool::Instance()->ReleaseConnection(pConn);
return Sdf_co_fail;
}
CdbConncetPool::Instance()->ReleaseConnection(pConn);
char szSql[128]长度是128,你的sql长度是176,strcpy会造成覆盖后面的内容,有很大风险。
好吧,我是猜的,C++早就还给老师了。。。
对的,你的就是正确答案,虽然我几天前就找到了,但是你的是正确答案