forked from handy1989/soci_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
125 lines (116 loc) · 3.12 KB
/
test.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <string>
#include <stdint.h>
#include "soci/soci.h"
using namespace soci;
using namespace std;
int g_pool_size = 3;
connection_pool g_pool(g_pool_size);
int insert(const string& first_name, const string& last_name)
{
session sql(g_pool);
try
{
sql << "insert into Person(first_name, last_name) values(:first_nam, :last_name)", use(first_name), use(last_name);
}
catch (const soci::soci_error& e)
{
cout << "err:" << e.what();
return -1;
}
long id;
if (!sql.get_last_insert_id("Person", id))
{
return -1;
}
cout << "insert into Person success" << endl
<< " id:" << id << endl
<< " first_name:" << first_name << endl
<< " last_name:" << last_name << endl;
return id;
}
void select(int id)
{
session sql(g_pool);
string first_name;
string last_name;
try
{
sql << "select first_name, last_name from Person where id=:id", use(id), into(first_name), into(last_name);
if (!sql.got_data())
{
cout << "select success, no record of id:" << id << endl;
return ;
}
}
catch (const soci::soci_error& e)
{
cout << "err:" << e.what();
return;
}
cout << "select success" << endl
<< " id:" << id << endl
<< " first_name:" << first_name << endl
<< " last_name:" << last_name << endl;
}
void update(int id, const string& first_name, const string& last_name)
{
session sql(g_pool);
try
{
sql << "update Person set first_name=:first_name, last_name=:last_name where id=:id", use(first_name), use(last_name), use(id);
}
catch (const soci::soci_error& e)
{
cout << "err:" << e.what() << endl;
return;
}
cout << "update success." << endl
<< " id:" << id << endl
<< " first_name:" << first_name << endl
<< " last_name:" << last_name << endl;
}
void remove(int id)
{
session sql(g_pool);
statement st = (sql.prepare << "delete from Person where id=:id", use(id));
st.execute(true);
int affected_rows = st.get_affected_rows();
cout << "delte success" << endl
<< " id:" << id << endl
<< " affected_rows:" << affected_rows << endl;
}
void init_pool()
{
for (int i = 0; i < g_pool_size; ++i)
{
session& sql = g_pool.at(i);
sql.open("mysql", "dbname=test user=zhangmenghan password=123456");
}
}
void select_all()
{
session sql(g_pool);
rowset<row> rs = (sql.prepare << "select * from Person");
cout << "select_all success" << endl;
for (rowset<row>::iterator it = rs.begin(); it != rs.end(); ++it)
{
cout << "----------------" << endl;
const row& row = *it;
cout << " id:" << row.get<long long>(0) << endl
<< " first_name:" << row.get<string>(1) << endl
<< " last_name:" << row.get<string>(2) << endl;
}
}
int main()
{
init_pool();
int id = insert("Steve", "Jobs");
select(id);
select_all();
update(id, "hello", "world");
select(id);
remove(id);
select(id);
return 0;
}