--- a/examples/standalone_example_docs2.cpp 2012-08-16 12:15:30.000000000 -0700 +++ b/examples/standalone_example_docs2.cpp 2012-08-23 10:05:54.000000000 -0700 @@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -35,6 +35,11 @@ */ #include "mysql_connection.h" +/* Public interface of the MySQL Connector/C++ */ +#include +/* Connection parameter and sample data */ +#include "examples.h" + #include #include #include @@ -43,59 +48,64 @@ using namespace std; -int main(void) +int main(int argc, const char **argv) { -cout << endl; -cout << "Let's have MySQL count from 10 to 1..." << endl; - -try { - sql::Driver *driver; - sql::Connection *con; - sql::Statement *stmt; - sql::ResultSet *res; - sql::PreparedStatement *pstmt; - - /* Create a connection */ - driver = sql::mysql::get_driver_instance(); - con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); - /* Connect to the MySQL test database */ - con->setSchema("test"); - - stmt = con->createStatement(); - stmt->execute("DROP TABLE IF EXISTS test"); - stmt->execute("CREATE TABLE test(id INT)"); - delete stmt; - - /* '?' is the supported placeholder syntax */ - pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)"); - for (int i = 1; i <= 10; i++) { - pstmt->setInt(1, i); - pstmt->executeUpdate(); - } - delete pstmt; - - /* Select in ascending order */ - pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC"); - res = pstmt->executeQuery(); - - /* Fetch in reverse = descending order! */ - res->afterLast(); - while (res->previous()) - cout << "\t... MySQL counts: " << res->getInt("id") << endl; - delete res; - - delete pstmt; - delete con; - -} catch (sql::SQLException &e) { - cout << "# ERR: SQLException in " << __FILE__; - cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl; - cout << "# ERR: " << e.what(); - cout << " (MySQL error code: " << e.getErrorCode(); - cout << ", SQLState: " << e.getSQLState() << " )" << endl; -} - -cout << endl; - -return EXIT_SUCCESS; + static const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST); + static const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); + static const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS); + static const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); + + cout << endl; + cout << "Let's have MySQL count from 10 to 1..." << endl; + + try { + sql::Driver *driver; + sql::Connection *con; + sql::Statement *stmt; + sql::ResultSet *res; + sql::PreparedStatement *pstmt; + + /* Create a connection */ + driver = sql::mysql::get_driver_instance(); + con = driver->connect(url, user, pass); + /* Connect to the MySQL test database */ + con->setSchema(database); + + stmt = con->createStatement(); + stmt->execute("DROP TABLE IF EXISTS test"); + stmt->execute("CREATE TABLE test(id INT)"); + delete stmt; + + /* '?' is the supported placeholder syntax */ + pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)"); + for (int i = 1; i <= 10; i++) { + pstmt->setInt(1, i); + pstmt->executeUpdate(); + } + delete pstmt; + + /* Select in ascending order */ + pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC"); + res = pstmt->executeQuery(); + + /* Fetch in reverse = descending order! */ + res->afterLast(); + while (res->previous()) + cout << "\t... MySQL counts: " << res->getInt("id") << endl; + delete res; + + delete pstmt; + delete con; + + } catch (sql::SQLException &e) { + cout << "# ERR: SQLException in " << __FILE__; + cout << "(" << EXAMPLE_FUNCTION << ") on line " << __LINE__ << endl; + cout << "# ERR: " << e.what(); + cout << " (MySQL error code: " << e.getErrorCode(); + cout << ", SQLState: " << e.getSQLState() << " )" << endl; + } + + cout << endl; + + return EXIT_SUCCESS; }