#include #include #include "include\mysql++" //Connection(cchar *db, cchar *host="",cchar *user="", cchar *passwd="") #define def_host_name "localhost" // host to connect to (default localhost) #define def_user_name "admin" // user name for db (default login name) #define def_password "" // password for the db (default is null) #define def_db_name "mysql_cpp_data" // default db name (default none) #define def_svr_port "3066" // port to connect to (defualt 3306) void QueryDatabases(); void ExecQuery(string sQuery); int main() { try { Connection con(def_db_name, def_host_name, def_user_name, def_password); Query query = con.query(); // This creates a query object that is bound to con. query << "select * from stock"; // You can write to the query object like you would any other ostrem cout << "Query: " << query.preview() << endl; // Show the query before it is executed // Query::preview() simply returns a string with the current query // string in it. Result res = query.store(); // Query::store() executes the query and returns the results Row row; cout.setf(ios::left); cout << setw(17) << "Item" << setw(4) << "Num" << setw(7) << "Weight" << setw(7) << "Price" << "Date" << endl << endl; Result::iterator i; // The Result class has a read-only Random Access Iterator for (i = res.begin(); i != res.end(); i++) { row = *i; // you can use either the index number or column name when // retrieving the colume data cout << setw(17) << row[0] << setw(4) << row[1] << setw(7) << row[2] << setw(7) << row[3] << row[4] << endl; } cout << endl << "Records Found: " << res.size() << endl; QueryDatabases(); // ExecQuery("select * from stock"); ExecQuery("show databases"); } catch (BadQuery er) { // handle any connection or query errors that may come up cerr << "Error: " << er.error << endl; return -1; } catch (BadConversion er) { // we still need to cache bad conversions incase something goes // wrong when the data is converted into stock cerr << "Error: Tried to convert \"" << er.data << "\" to a \"" << er.type_name << "\"." << endl; return -1; } cout << endl << endl << "Press any key to exit..."; getchar(); return 0; } void QueryDatabases() { Connection con(def_db_name, def_host_name, def_user_name, def_password); Query query = con.query(); query << "show databases"; cout << endl << "Query: " << query.preview() << endl; Result res = query.store(); Row row; cout.setf(ios::left); Result::iterator i; // The Result class has a read-only Random Access Iterator for (i = res.begin(); i != res.end(); i++) { row = *i; cout <