表mydb.mytable
--------------------------------------------------------
| sid(BIGINT) | Name(VARCHAR(50)) |
--------------------------------------------------------
【存储过程1】通过参数返回一条数据
CREATE DEFINER=`root`@`%` PROCEDURE `Query`(IN `skey` VARCHAR(50), OUT `oid` BIGINT, OUT `oname` VARCHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select sid, Name into oid, oname from mydb.mytable where Name=skey limit 0,1;
END
Connector c++调用
Driver *pDriver = mysql::get_mysql_driver_instance();
try
{
auto_ptr pConnection(pDriver->connect("tcp://127.0.0.1:3306", "root", "123456"));
auto_ptrprepQuery(pConnection->prepareStatement(
"call searchserver.query(?, @sid, @sname)" ));
prepQuery->setString(1, "ABC");
prepQuery->execute();
auto_ptrprepSelect(pConnection->prepareStatement(
"select @sid as sid, @sname as sname"));
auto_ptrlpRes(prepSelect->executeQuery());
int nRows = lpRes->rowsCount();
while(lpRes->next())
{
uint64_t ID = lpRes->getUInt64(1);
string strName = lpRes->getString(2);
}
}
catch (SQLException& e)
{
string str = e.what();
return;
}
【存储过程2】默认返回记录集
CREATE DEFINER=`root`@`%` PROCEDURE `Query`(IN `skey` VARCHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select sid, Name from mydb.mytable where Name like skey;
END
Connector c++调用
Driver *pDriver = mysql::get_mysql_driver_instance();
try
{
auto_ptr pConnection(pDriver->connect("tcp://127.0.0.1:3306", "root", "123456"));
auto_ptrprepQuery(pConnection->prepareStatement("call mydb.query(?)" ));
prepQuery->setString(1, "A%");
auto_ptrlpRes(prepQuery->executeQuery());
int nRows = lpRes->rowsCount();
while(lpRes->next())
{
uint64_t ID = lpRes->getUInt64(1);
string strName = lpRes->getString(2);
}
}
catch (SQLException& e)
{
string str = e.what();
return;
}
参考:/kf/08/100908.html