Esercizi Lettura MySQL C++
- Esercizio 1: Lettura di un singolo record usando MySQL Connector/C++ (metodo sequenziale)
- Esercizio 2: Lettura di più record usando MySQL Connector/C++ (metodo sequenziale)
- Esercizio 3: Lettura di un singolo record usando MySQL Connector/C++ (OOP)
- Esercizio 4: Lettura di più record usando MySQL Connector/C++ (OOP)
- Esercizio 5: Lettura di un singolo record usando MySQL C API (metodo sequenziale)
- Esercizio 6: Lettura di più record usando MySQL C API (OOP)
Ecco degli esercizi semplici con soluzione per praticare la lettura di dati in MySQL utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Lettura di un singolo record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL e leggere un singolo record.
#include <mysql_driver.h>#include <mysql_connection.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>#include <cppconn/resultset.h>#include <iostream>
int main() { sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance(); std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "password")); con->setSchema("test_db");
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement("SELECT * FROM test_table WHERE name = ?")); pstmt->setString(1, "John Doe"); std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
if (res->next()) { std::cout << "ID: " << res->getInt("id") << ", Name: " << res->getString("name") << ", Age: " << res->getInt("age") << std::endl; } else { std::cout << "Record non trovato" << std::endl; }
return 0;}
Esercizio 2: Lettura di più record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL e leggere più record.
#include <mysql_driver.h>#include <mysql_connection.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>#include <cppconn/resultset.h>#include <iostream>
int main() { sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance(); std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "password")); con->setSchema("test_db");
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement("SELECT * FROM test_table WHERE age > ?")); pstmt->setInt(1, 20); std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
while (res->next()) { std::cout << "ID: " << res->getInt("id") << ", Name: " << res->getString("name") << ", Age: " << res->getInt("age") << std::endl; }
return 0;}
Esercizio 3: Lettura di un singolo record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL e leggere un singolo record.
#include <mysql_driver.h>#include <mysql_connection.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>#include <cppconn/resultset.h>#include <iostream>
class MySQLClient {private: sql::mysql::MySQL_Driver* driver; std::unique_ptr<sql::Connection> con;public: MySQLClient(const std::string& host, const std::string& user, const std::string& pass, const std::string& db) { driver = sql::mysql::get_mysql_driver_instance(); con.reset(driver->connect(host, user, pass)); con->setSchema(db); }
void leggiRecordSingolo(const std::string& query, const std::string& name) { std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement(query)); pstmt->setString(1, name); std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
if (res->next()) { std::cout << "ID: " << res->getInt("id") << ", Name: " << res->getString("name") << ", Age: " << res->getInt("age") << std::endl; } else { std::cout << "Record non trovato" << std::endl; } }};
int main() { MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db"); client.leggiRecordSingolo("SELECT * FROM test_table WHERE name = ?", "John Doe"); return 0;}
Esercizio 4: Lettura di più record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL e leggere più record.
#include <mysql_driver.h>#include <mysql_connection.h>#include <cppconn/statement.h>#include <cppconn/prepared_statement.h>#include <cppconn/resultset.h>#include <iostream>
class MySQLClient {private: sql::mysql::MySQL_Driver* driver; std::unique_ptr<sql::Connection> con;public: MySQLClient(const std::string& host, const std::string& user, const std::string& pass, const std::string& db) { driver = sql::mysql::get_mysql_driver_instance(); con.reset(driver->connect(host, user, pass)); con->setSchema(db); }
void leggiRecordMultipli(const std::string& query, int age) { std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement(query)); pstmt->setInt(1, age); std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
while (res->next()) { std::cout << "ID: " << res->getInt("id") << ", Name: " << res->getString("name") << ", Age: " << res->getInt("age") << std::endl; } }};
int main() { MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db"); client.leggiRecordMultipli("SELECT * FROM test_table WHERE age > ?", 20); return 0;}
Esercizio 5: Lettura di un singolo record usando MySQL C API (metodo sequenziale)
Stabilire una connessione a MySQL e leggere un singolo record usando MySQL C API.
#include <mysql/mysql.h>#include <iostream>
int main() { MYSQL* conn = mysql_init(nullptr); if (conn == nullptr) { std::cerr << "mysql_init() failed\n"; return EXIT_FAILURE; }
if (mysql_real_connect(conn, "localhost", "root", "password", "test_db", 0, nullptr, 0) == nullptr) { std::cerr << "mysql_real_connect() failed\n"; mysql_close(conn); return EXIT_FAILURE; }
const char* query = "SELECT * FROM test_table WHERE name = 'John Doe'"; if (mysql_query(conn, query)) { std::cerr << "SELECT failed. Error: " << mysql_error(conn) << std::endl; mysql_close(conn); return EXIT_FAILURE; }
MYSQL_RES* res = mysql_store_result(conn); if (res == nullptr) { std::cerr << "mysql_store_result() failed. Error: " << mysql_error(conn) << std::endl; mysql_close(conn); return EXIT_FAILURE; }
MYSQL_ROW row; if ((row = mysql_fetch_row(res))) { std::cout << "ID: " << row[0] << ", Name: " << row[1] << ", Age: " << row[2] << std::endl; } else { std::cout << "Record non trovato" << std::endl; }
mysql_free_result(res); mysql_close(conn); return EXIT_SUCCESS;}
Esercizio 6: Lettura di più record usando MySQL C API (OOP)
<
details class=“mb-10”>
#include <mysql/mysql.h>#include <iostream>#include <stdexcept>
class MySQLClient {private: MYSQL* conn;public: MySQLClient(const std::string& host, const std::string& user, const std::string& pass, const std::string& db) { conn = mysql_init(nullptr); if (conn == nullptr) { throw std::runtime_error("mysql_init() failed"); } if (mysql_real_connect(conn, host.c_str(), user.c_str(), pass.c_str(), db.c_str(), 0, nullptr, 0) == nullptr) { mysql_close(conn); throw std::runtime_error("mysql_real_connect() failed"); } }
~MySQLClient() { mysql_close(conn); }
void leggiRecordMultipli(const std::string& query, int age) { std::string full_query = query + std::to_string(age); if (mysql_query(conn, full_query.c_str())) { throw std::runtime_error("SELECT failed: " + std::string(mysql_error(conn))); }
MYSQL_RES* res = mysql_store_result(conn); if (res == nullptr) { throw std::runtime_error("mysql_store_result() failed: " + std::string(mysql_error(conn))); }
MYSQL_ROW row; while ((row = mysql_fetch_row(res))) { std::cout << "ID: " << row[0] << ", Name: " << row[1] << ", Age: " << row[2] << std::endl; }
mysql_free_result(res); }};
int main() { try { MySQLClient client("localhost", "root", "password", "test_db"); client.leggiRecordMultipli("SELECT * FROM test_table WHERE age > ", 20); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; } return 0;}