/*********************************************************************** simple1.cpp - Example showing the simplest way to get data from a MySQL table with MySQL++. Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and (c) 2004-2009 by Educational Technology Resources, Inc. Others may also hold copyrights on code in this file. See the CREDITS.txt 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 ***********************************************************************/ #include "cmdline.h" #include "printdata.h" #include #include #include #include using namespace std; #define USE_FULL_SSQLS_DECLARATION #define kTHE_MYSQL_DECLARATION "t_events ( \ id int auto_increment, \ updated timestamp default current_timestamp, \ sometext varchar(255), \ PRIMARY KEY (id) \ ) ENGINE=MyISAM DEFAULT CHARSET=utf8" #ifdef USE_FULL_SSQLS_DECLARATION sql_create_3(t_events, 1, 3, mysqlpp::sql_int, id, mysqlpp::sql_timestamp, updated, mysqlpp::sql_varchar, sometext); #else // use a sub-table sql_create_2(t_events, 1, 2, mysqlpp::sql_int, id, mysqlpp::sql_varchar, sometext); #endif int main(int argc, char *argv[]) { // Get database access parameters from command line mysqlpp::examples::CommandLine cmdline(argc, argv); if (!cmdline) { return 1; } try { std::cout << "Connecting to server..." << std::endl; mysqlpp::Connection con; con.connect(mysqlpp::examples::db_name, cmdline.server(), cmdline.user(), cmdline.pass()); { mysqlpp::Query query = con.query(); std::cout << "Dropping old table..." << std::endl; query << "drop table if exists t_events"; query.execute(); std::cout << "Creating new table..." << std::endl; query << "create table " << kTHE_MYSQL_DECLARATION; query.execute(); } { std::cout << "Inserting a row..." << std::endl; mysqlpp::Query query = con.query(); t_events row; row.id = 0; #ifdef USE_FULL_SSQLS_DECLARATION std::cout << "Using the full declaration - expecting MySQL to update the timestamp..." << std::endl; row.updated = mysqlpp::sql_timestamp(); #else std::cout << "Using a sub-declaration - letting MySQL do its thing..." << std::endl; #endif row.sometext = "Hello World!"; query.insert(row); std::cout << "QUERY: " << query << std::endl; query.execute(); } { std::cout << "Selecting the row..." << std::endl; mysqlpp::Query query = con.query(); query << "select * from t_events"; mysqlpp::StoreQueryResult res = query.store(); std::cout << "RESULT: id = " << res[0]["id"] << ", updated = " << res[0]["updated"] << ", sometext = " << res[0]["sometext"] << std::endl; } } catch (const mysqlpp::Exception& er) { std::cout << "ERROR: " << er.what() << std::endl; return 1; } return 0; }