Esercizi Inserimento MySQL C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare l’inserimento singolo e multiplo di record in MySQL utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Inserimento singolo di un record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL ed inserire 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("INSERT INTO test_table (name, age) VALUES (?, ?)"));
pstmt->setString(1, "John Doe");
pstmt->setInt(2, 30);
pstmt->executeUpdate();
std::cout << "Record inserito con successo." << std::endl;
return 0;
}
Esercizio 2: Inserimento multiplo di record usando MySQL Connector/C++ (metodo sequenziale)
Stabilire una connessione a MySQL ed inserire 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("INSERT INTO test_table (name, age) VALUES (?, ?)"));
pstmt->setString(1, "Alice");
pstmt->setInt(2, 25);
pstmt->executeUpdate();
pstmt->setString(1, "Bob");
pstmt->setInt(2, 28);
pstmt->executeUpdate();
std::cout << "Record multipli inseriti con successo." << std::endl;
return 0;
}
Esercizio 3: Inserimento singolo di un record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL ed inserire 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 inserisciRecordSingolo(const std::string& table, const std::string& name, int age) {
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement("INSERT INTO " + table + " (name, age) VALUES (?, ?)"));
pstmt->setString(1, name);
pstmt->setInt(2, age);
pstmt->executeUpdate();
std::cout << "Record inserito con successo." << std::endl;
}
};
int main() {
MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db");
client.inserisciRecordSingolo("test_table", "John Doe", 30);
return 0;
}
Esercizio 4: Inserimento multiplo di record usando MySQL Connector/C++ (OOP)
Creare una classe per gestire la connessione a MySQL ed inserire 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 inserisciRecordMultipli(const std::string& table, const std::vector<std::pair<std::string, int>>& records) {
std::unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement("INSERT INTO " + table + " (name, age) VALUES (?, ?)"));
for (const auto& record : records) {
pstmt->setString(1, record.first);
pstmt->setInt(2, record.second);
pstmt->executeUpdate();
}
std::cout << "Record multipli inseriti con successo." << std::endl;
}
};
int main() {
MySQLClient client("tcp://127.0.0.1:3306", "root", "password", "test_db");
std::vector<std::pair<std::string, int>> records = {{"Alice", 25}, {"Bob", 28}};
client.inserisciRecordMultipli("test_table", records);
return 0;
}
Esercizio 5: Inserimento singolo di un record usando MySQL C API (metodo sequenziale)
Stabilire una connessione a MySQL ed inserire 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 = "INSERT INTO test_table (name, age) VALUES ('John Doe', 30)";
if (mysql_query(conn, query)) {
std::cerr << "INSERT failed. Error: " << mysql_error(conn) << std::endl;
} else {
std::cout << "Record inserito con successo." << std::endl;
}
mysql_close(conn);
return EXIT_SUCCESS;
}
Esercizio 6: Inserimento multiplo di record usando MySQL C API (metodo sequenziale)
Stabilire una connessione a MySQL ed inserire più 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* query1 = "INSERT INTO test_table (name, age) VALUES ('Alice', 25)";
const char* query2 = "INSERT INTO test_table (name, age) VALUES ('Bob', 28)";
if (mysql_query(conn, query1)) {
std::cerr << "INSERT failed. Error
: " << mysql_error(conn) << std::endl;
}
if (mysql_query(conn, query2)) {
std::cerr << "INSERT failed. Error: " << mysql_error(conn) << std::endl;
} else {
std::cout << "Record multipli inseriti con successo." << std::endl;
}
mysql_close(conn);
return EXIT_SUCCESS;
}
Esercizio 7: Inserimento singolo di un record usando MySQL C API (OOP)
Creare una classe per gestire la connessione a MySQL ed inserire un singolo 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 inserisciRecordSingolo(const std::string& table, const std::string& name, int age) {
std::string query = "INSERT INTO " + table + " (name, age) VALUES ('" + name + "', " + std::to_string(age) + ")";
if (mysql_query(conn, query.c_str())) {
throw std::runtime_error("INSERT failed: " + std::string(mysql_error(conn)));
} else {
std::cout << "Record inserito con successo." << std::endl;
}
}
};
int main() {
try {
MySQLClient client("localhost", "root", "password", "test_db");
client.inserisciRecordSingolo("test_table", "John Doe", 30);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
Esercizio 8: Inserimento multiplo di record usando MySQL C API (OOP)
Creare una classe per gestire la connessione a MySQL ed inserire più record usando MySQL C API.
#include <mysql/mysql.h>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <utility>
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 inserisciRecordMultipli(const std::string& table, const std::vector<std::pair<std::string, int>>& records) {
for (const auto& record : records) {
std::string query = "INSERT INTO " + table + " (name, age) VALUES ('" + record.first + "', " + std::to_string(record.second) + ")";
if (mysql_query(conn, query.c_str())) {
throw std::runtime_error("INSERT failed: " + std::string(mysql_error(conn)));
}
}
std::cout << "Record multipli inseriti con successo." << std::endl;
}
};
int main() {
try {
MySQLClient client("localhost", "root", "password", "test_db");
std::vector<std::pair<std::string, int>> records = {{"Alice", 25}, {"Bob", 28}};
client.inserisciRecordMultipli("test_table", records);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}