// // Copyright (c) 2019-2022 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // //[tutorial_listing #include #include #include #include #include #include /** * For this example, we will be using the 'boost_mysql_examples' database. * You can get this database by running db_setup.sql. * This example assumes you are connecting to a localhost MySQL server. * * This example uses synchronous functions and handles errors using exceptions. */ void main_impl(int argc, char** argv) { if (argc != 4) { std::cerr << "Usage: " << argv[0] << " \n"; exit(1); } //[tutorial_connection // The execution context, required to run I/O operations. boost::asio::io_context ctx; // The SSL context, required to establish TLS connections. // The default SSL options are good enough for us at this point. boost::asio::ssl::context ssl_ctx(boost::asio::ssl::context::tls_client); // Represents a connection to the MySQL server. boost::mysql::tcp_ssl_connection conn(ctx.get_executor(), ssl_ctx); //] //[tutorial_connect // Resolve the hostname to get a collection of endpoints boost::asio::ip::tcp::resolver resolver(ctx.get_executor()); auto endpoints = resolver.resolve(argv[3], boost::mysql::default_port_string); // The username and password to use boost::mysql::handshake_params params( argv[1], // username argv[2] // password ); // Connect to the server using the first endpoint returned by the resolver conn.connect(*endpoints.begin(), params); //] //[tutorial_query // Issue the SQL query to the server const char* sql = "SELECT \"Hello world!\""; boost::mysql::tcp_ssl_resultset result; conn.query(sql, result); //] //[tutorial_read // Read the query results into memory boost::mysql::rows all_rows; result.read_all(all_rows); //] //[tutorial_fields // Print the first field in the first row std::cout << all_rows.at(0).at(0) << std::endl; //] //[tutorial_close conn.close(); //] } int main(int argc, char** argv) { try { main_impl(argc, argv); } catch (const boost::system::system_error& err) { std::cerr << "Error: " << err.what() << ", error code: " << err.code() << std::endl; return 1; } catch (const std::exception& err) { std::cerr << "Error: " << err.what() << std::endl; return 1; } } //]