--- a/examples/standalone_example_docs1.cpp 2012-08-16 12:15:30.000000000 -0700 +++ b/examples/standalone_example_docs1.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 @@ -42,46 +47,52 @@ using namespace std; -int main(void) +int main(int argc, const char **argv) { -cout << endl; -cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl; - -try { - sql::Driver *driver; - sql::Connection *con; - sql::Statement *stmt; - sql::ResultSet *res; - - /* 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(); - res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); - while (res->next()) { - cout << "\t... MySQL replies: "; - /* Access column data by alias or column name */ - cout << res->getString("_message") << endl; - cout << "\t... MySQL says it again: "; - /* Access column fata by numeric offset, 1 is the first column */ - cout << res->getString(1) << endl; - } - delete res; - delete stmt; - 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; -} + 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 << "Running 'SELECT 'Hello World!' AS _message'..." << endl; + + try { + sql::Driver *driver; + sql::Connection *con; + sql::Statement *stmt; + sql::ResultSet *res; + + /* 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(); + res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); + while (res->next()) { + cout << "\t... MySQL replies: "; + /* Access column data by alias or column name */ + cout << res->getString("_message") << endl; + cout << "\t... MySQL says it again: "; + /* Access column fata by numeric offset, 1 is the first column */ + cout << res->getString(1) << endl; + } + delete res; + delete stmt; + 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; + cout << endl; -return EXIT_SUCCESS; + return EXIT_SUCCESS; }