MySQL Connector/C++ 调用存储过程

MySQL Connector/C++ 调用存储过程
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;

你可能感兴趣的:(MySQL Connector/C++ 调用存储过程)