7.2.1Statement对不返回结果的存储过程使用 a

此示例说明如何调用不返回结果集的存储过程。

  1. 复制教程框架代码:

    $> cp framework.cpp sp_scenario1.cpp
  2. 将以下代码添加到try教程框架的块中:

    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());
    
    // We need not check the return value explicitly. If it indicates
    // an error, Connector/C++ generates an exception.
    stmt->execute("CALL add_country('ATL', 'Atlantis', 'North America')");
  3. 按照第 7.1 节“先决条件和背景信息” 中的说明编译程序 。

  4. 运行程序:

    $> ./sp_scenario1
  5. 使用mysql命令行客户端或其他合适的程序,检查world 数据库以确定它已正确更新。您可以使用此查询:

    mysql> SELECT Code, Name, Continent FROM Country WHERE Code='ATL';
    +------+----------+---------------+
    | Code | Name     | Continent     |
    +------+----------+---------------+
    | ATL  | Atlantis | North America |
    +------+----------+---------------+

此应用程序中的代码只是调用该 execute方法,将调用存储过程的语句传递给它。过程本身不返回任何值,但重要的是要注意CALL语句总是有一个返回值;这是execute状态。Connector/C++ 会为您处理此状态,因此您无需显式处理它。如果execute调用由于某种原因失败,它会引发catch块处理的异常。