/*********************************************************************** simple1.cpp - Example showing the simplest way to get data from a MySQL table with MySQL++. Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by MySQL AB, and (c) 2004-2007 by Educational Technology Resources, Inc. Others may also hold copyrights on code in this file. See the CREDITS file in the top directory of the distribution for details. This file is part of MySQL++. MySQL++ is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. MySQL++ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with MySQL++; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Modifications: M. Van Gorp Nov 2007 ***********************************************************************/ #include #include #include using namespace std; int main() { mysqlpp::Connection con; //Create variable con of type Connection mysqlpp::Result res; //Create variable res of type Result // Connect to database try { // database name host name user passwd con.connect("mysql_cpp_data","localhost", "root", "olathe"); } //Catch error er as type ConnectionFailed catch (const mysqlpp::ConnectionFailed& er) { cerr << "Connection failed: " << er.what() << endl; return EXIT_FAILURE; } // Retrieve a subset of the sample stock table //Create variable query of type Query and set it to the current query. mysqlpp::Query query = con.query(); query << "select * from stock"; //Change the query res = query.store(); //Set the Result res to the result of executing //the query, which is a part of the table. // Display the result set cout << "We have:" << endl; if (res) { //If Result res is not "blank" mysqlpp::Row row; //Create variable row of type Row //Create variable i of type Row::size_type for (mysqlpp::Row::size_type i = 0; i < res.rows(); ++i) { row = res.at(i); //Set row to the row at index i, which in this case //only contains one column. cout << '\t' << row.at(0) << endl; //Display the first column in //the row. } } else { cerr << "Failed to get item list: " << query.error() << endl; return 1; } return EXIT_SUCCESS; }