#include #include #include int main() { // The full format for the Connection constructor is // Connection(cchar *db, cchar *host="", // cchar *user="", cchar *passwd="") // You may need to specify some of them if the database is not on // the local machine or your database username is not the same as your // login name, etc.. try { mysqlcppapi::Connection con; con.connect(); con.select_database("mysql_cpp_data"); mysqlcppapi::Query query = con.create_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 mysqlcppapi::Result_Store res = query.store(); // Query::store() executes the query and returns the results std::cout << "Query: " << query.preview() << std::endl; // Query::preview() simply returns a string with the current query // string in it. std::cout << "Records Found: " << res.size() << std::endl << std::endl; std::cout.setf(std::ios::left); std::cout << std::setw(17) << "Item" << std::setw(4) << "Num" << std::setw(7) << "Weight" << std::setw(7) << "Price" << "Date" << std::endl << std::endl; // The Result_Store class has a read-only Random Access Iterator for (mysqlcppapi::Result_Store::iterator i = res.begin(); i != res.end(); i++) { mysqlcppapi::Row row = *i; std::cout << std::setw(17) << row[0] << std::setw(4) << row[1] << std::setw(7) << row["weight"] // you can use either the index number or column name when // retrieving the colume data as demonstrated above. << std::setw(7) << row[3] << row[4] << std::endl; } } catch(mysqlcppapi::ex_BadQuery& er) { // handle any connection or query errors that may come up std::cerr << "Error: " << er.what() << std::endl; return -1; } catch(mysqlcppapi::ex_BadConversion& er) { // we still need to catch bad conversions in case something goes // wrong when the data is converted into stock std::cerr << "Error: Tried to convert \"" << er.get_Data() << "\" to a \"" << er.get_TypeName() << "\"." << std::endl; return -1; } return 0; }