Esercizi Modifica MySQL C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare la modifica di dati in MySQL utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Modifica di un singolo record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL e modificare un singolo record.
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.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("UPDATE test_table SET age = ? WHERE name = ?"));
pstmt->setInt(1, 35);
pstmt->setString(2, "John Doe");
int rows = pstmt->executeUpdate();
std::cout << "Record modificati: " << rows << std::endl;
return 0;
}
Esercizio 2: Modifica di più record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL e modificare più record.
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.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("UPDATE test_table SET status = ? WHERE status = ?"));
pstmt->setString(1, "inactive");
pstmt->setString(2, "active");
int rows = pstmt->executeUpdate();
std::cout << "Record modificati: " << rows << std::endl;
return 0;
}
Esercizio 3: Modifica di un singolo record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL e modificare un singolo record.
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.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 modificaRecord(const std::string& query, int age, const std::string& name) {
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement(query));
pstmt->setInt(1, age);
pstmt->setString(2, name);
int rows = pstmt->executeUpdate();
std::cout << "Record modificati: " << rows << std::endl;
}
};
int main() {
MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db");
client.modificaRecord("UPDATE test_table SET age = ? WHERE name = ?", 35, "John Doe");
return 0;
}
Esercizio 4: Modifica di più record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL e modificare più record.
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.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 modificaRecords(const std::string& query, const std::string& new_status, const std::string& old_status) {
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement(query));
pstmt->setString(1, new_status);
pstmt->setString(2, old_status);
int rows = pstmt->executeUpdate();
std::cout << "Record modificati: " << rows << std::endl;
}
};
int main() {
MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db");
client.modificaRecords("UPDATE test_table SET status = ? WHERE status = ?", "inactive", "active");
return 0;
}
Esercizio 5: Modifica di un singolo record usando MySQL C API (metodo sequenziale)
Stabilire una connessione a MySQL e modificare 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 = "UPDATE test_table SET age = 35 WHERE name = 'John Doe'";
if (mysql_query(conn, query)) {
std::cerr << "UPDATE failed. Error: " << mysql_error(conn) << std::endl;
} else {
std::cout << "Record modificato con successo." << std::endl;
}
mysql_close(conn);
return EXIT_SUCCESS;
}
Esercizio 6: Modifica di più record usando MySQL C API (OOP)
Creare una classe per gestire la connessione a MySQL e modificare più record usando MySQL C API.
#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 modificaRecords(const std::string& query) {
if (mysql_query(conn, query.c_str())) {
throw std::runtime_error("UPDATE failed: " + std::string(mysql_error(conn)));
} else {
std::cout << "Record modificati con successo." << std::endl;
}
}
};
int main() {
try {
MySQLClient client("localhost", "root", "password", "test_db");
client.modificaRecords("UPDATE test_table SET status = 'inactive' WHERE status =
'active'");
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}