-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
52 lines (42 loc) · 1.56 KB
/
main.cpp
File metadata and controls
52 lines (42 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/exception.h>
#include <iostream>
int main() {
try {
sql::mysql::MySQL_Driver* driver;
sql::Connection* con;
sql::PreparedStatement* pstmt;
// Création de l'objet driver
driver = sql::mysql::get_mysql_driver_instance();
// Connexion à la base de données
con = driver->connect("tcp://127.0.0.1:3306", "testuser", "password");
// Sélection de la base de données
con->setSchema("testdb");
// Création de la table si elle n'existe pas
pstmt = con->prepareStatement("CREATE TABLE IF NOT EXISTS PERSON("
"ID INT PRIMARY KEY AUTO_INCREMENT, "
"NAME VARCHAR(255), "
"AGE INT)");
pstmt->execute();
delete pstmt;
// Préparation de l'insertion de données
pstmt = con->prepareStatement("INSERT INTO PERSON (NAME, AGE) VALUES (?, ?)");
pstmt->setString(1, "Alice");
pstmt->setInt(2, 25);
pstmt->execute();
pstmt->setString(1, "Bob");
pstmt->setInt(2, 30);
pstmt->execute();
std::cout << "Données insérées avec succès." << std::endl;
// Libération des ressources
delete pstmt;
delete con;
} catch (sql::SQLException& e) {
std::cerr << "Erreur MySQL : " << e.what() << std::endl;
return 1;
}
return 0;
}