--------------------------------------------------------
【存储过程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
Driver *pDriver = mysql::get_mysql_driver_instance(); try { auto_ptr<Connection> pConnection(pDriver->connect("tcp://127.0.0.1:3306", "root", "123456")); auto_ptr<PreparedStatement>prepQuery(pConnection->prepareStatement( "call searchserver.query(?, @sid, @sname)" )); prepQuery->setString(1, "ABC"); prepQuery->execute(); auto_ptr<PreparedStatement>prepSelect(pConnection->prepareStatement( "select @sid as sid, @sname as sname")); auto_ptr<ResultSet>lpRes(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
Driver *pDriver = mysql::get_mysql_driver_instance(); try { auto_ptr<Connection> pConnection(pDriver->connect("tcp://127.0.0.1:3306", "root", "123456")); auto_ptr<PreparedStatement>prepQuery(pConnection->prepareStatement("call mydb.query(?)" )); prepQuery->setString(1, "A%"); auto_ptr<ResultSet>lpRes(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; }