Documentation Home
MySQL Connector/C++ Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb
EPUB - 0.7Mb
HTML Download (TGZ) - 0.6Mb
HTML Download (Zip) - 0.7Mb


7.4 Using Prepared Statements

If you are not familiar with Prepared Statements in MySQL, take a look at the source code comments and explanations in the file examples/prepared_statement.cpp.

sql::PreparedStatement is created by passing an SQL query to sql::Connection::prepareStatement(). As sql::PreparedStatement is derived from sql::Statement, you will feel familiar with the API once you have learned how to use (simple) statements (sql::Statement). For example, the syntax for fetching results is identical.

// ...
sql::Connection *con;
sql::PreparedStatement  *prep_stmt
// ...

prep_stmt = con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)");

prep_stmt->setInt(1, 1);
prep_stmt->setString(2, "a");
prep_stmt->execute();

prep_stmt->setInt(1, 2);
prep_stmt->setString(2, "b");
prep_stmt->execute();

delete prep_stmt;
delete con;
Note

You must free the sql::PreparedStatement and sql::Connection objects explicitly using delete.


User Comments
  Posted by Atul Patil on December 7, 2015
Please note, memory might stay allocated to result set pointer despite we delete it and it remains allocated till we delete prep_stmt.
(For more details please refer: http://stackoverflow.com/questions/34137239/prepared-statements-memory-stays-allocated-to-results-despite-we-delete-it)
Sign Up Login You must be logged in to post a comment.