This example shows how to call a stored procedure that returns no result set.
-
Make a copy of the tutorial framework code:
$> cp framework.cpp sp_scenario1.cpp
-
Add the following code to the
try
block of the tutorial framework: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')");
Compile the program as described in Section 7.1, “Prerequisites and Background Information”.
-
Run the program:
$> ./sp_scenario1
-
Using the mysql command-line client or other suitable program, check the
world
database to determine that it has been updated correctly. You can use this query:mysql> SELECT Code, Name, Continent FROM Country WHERE Code='ATL'; +------+----------+---------------+ | Code | Name | Continent | +------+----------+---------------+ | ATL | Atlantis | North America | +------+----------+---------------+
The code in this application simply invokes the
execute
method, passing to it a statement
that calls the stored procedure. The procedure itself returns no
value, although it is important to note there is always a return
value from the CALL
statement;
this is the execute
status. Connector/C++ handles
this status for you, so you need not handle it explicitly. If
the execute
call fails for some reason, it
raises an exception that the catch
block
handles.