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
53
|
#include <mysql++.h>
#include <ssqls.h>
// 定义数据表结构
sql_create_3(MyTable, 1, 3,
mysqlpp::sql_int, id,
mysqlpp::sql_varchar, name,
mysqlpp::sql_int, age)
int main()
{
// 创建一个MySQL对象并连接到数据库
mysqlpp::Connection conn(false);
if (conn.connect("database_name", "localhost", "username", "password")) {
// 连接成功
// 创建一个查询对象
mysqlpp::Query query = conn.query();
// 执行查询操作
query << "SELECT * FROM my_table";
mysqlpp::StoreQueryResult res = query.store();
if (res) {
// 查询成功
// 遍历结果集
for (size_t i = 0; i < res.num_rows(); ++i) {
const MyTable& row = res[i];
std::cout << "ID: " << row.id << ", Name: " << row.name << ", Age: " << row.age << std::endl;
}
} else {
// 查询失败
std::cerr << "Query error: " << query.error() << std::endl;
}
// 插入数据
MyTable newRow(0, "John Doe", 25);
query.insert(newRow);
if (mysqlpp::SimpleResult result = query.execute()) {
// 插入成功
std::cout << "Insert successful. Affected rows: " << result.rows() << std::endl;
} else {
// 插入失败
std::cerr << "Insert error: " << query.error() << std::endl;
}
} else {
// 连接失败
std::cerr << "Connection error: " << conn.error() << std::endl;
}
return 0;
}
|