金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
MySQL Connector/C++ 调用存储过程

(金庆的专栏)

摘自:
http://dev.mysql.com/doc/refman/5.6/en/connector-cpp-tutorials-stored-routines-statements.html

CREATE PROCEDURE test (IN country_code CHAR(3), OUT country_pop INT(11))
BEGIN
    INSERT INTO Country(Code) VALUES (country_code);

    SELECT Population INTO country_pop FROM Country WHERE Name = 'country_name';

    SELECT Code, Name, Population, Continent FROM Country WHERE Continent = "Oceania" AND Population < 10000;
    SELECT Code, Name, Population, Continent FROM Country WHERE Continent = "Europe" AND Population < 10000;
    SELECT Code, Name, Population, Continent FROM Country WHERE Continent = "North America" AND Population < 10000;
END


sql::Driver* driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect(url, user, pass));
con->setSchema(database);
std::auto_ptr<sql::Statement> stmt(con->createStatement());

stmt->execute("CALL test(\"ATL\", @pop)");  

std::auto_ptr< sql::ResultSet > res;
do {
   res.reset(stmt->getResultSet());
   while (res->next()) {
      cout << "Result: " << res->getString(1) << endl;
   }
} while (stmt->getMoreResults());

std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT @pop AS _reply"));
while (res->next())
   cout << "Population of Uganda: " << res->getString("_reply") << endl;
posted on 2012-04-13 10:25 金庆 阅读(1592) 评论(0)  编辑 收藏 引用 所属分类: 1. C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理