-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
117 lines (93 loc) · 2.19 KB
/
main.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
#include <string>
#include <iostream>
#include "mysqlmgr.hpp"
#pragma comment(lib,"libmysql.lib")
#pragma comment(lib,"ws2_32.lib")
using namespace std;
const string DROP_SAMPLE_TABLE = "DROP TABLE IF EXISTS test_table";
const string CREATE_SAMPLE_TABLE = "CREATE TABLE test_table(id int, name VARCHAR(40))";
const string INSERT_SAMPLE = "INSERT INTO test_table(id, name) VALUES(?,?)";
const string QUERY_SAMPLE = "SELECT * FROM test_table";
void drop()
{
auto &sqlmgr = Mysqlmgr::getinstance();
int ret = sqlmgr.execSQL(DROP_SAMPLE_TABLE);
if (ret == -1)
{
printf("drop table error! %s\n", sqlmgr.geterror());
}
printf("drop sucess.\n");
}
void create()
{
auto &sqlmgr = Mysqlmgr::getinstance();
int ret = sqlmgr.execSQL(CREATE_SAMPLE_TABLE);
if (ret == -1)
{
printf("create table error! %s\n", sqlmgr.geterror());
}
printf("create table sucess.\n");
}
int insert()
{
auto &sqlmgr = Mysqlmgr::getinstance();
auto stmt = sqlmgr.getstmt(INSERT_SAMPLE);
if (!stmt)
{
printf("insert error: %s", sqlmgr.geterror());
return false;
}
for (int i = 0; i < 3; i++)
{
stmt.bindint(0, i);
string name = "smith" + to_string(i);
stmt.bindstring(1, name);
if (stmt.execute())
{
printf("insert error: %s", sqlmgr.geterror());
}
}
printf("insert sucess.\n");
}
int query()
{
auto &sqlmgr = Mysqlmgr::getinstance();
Result r = sqlmgr.query(QUERY_SAMPLE);
if (!r)
{
printf("query error %s\n", sqlmgr.geterror());
return -1;
}
printf("query result:\n");
while (Row row = r.next())
{
auto id = row.columnint(0);
auto name = row.columntext(1);
printf("id:%d, name: %s\n", id, name.c_str());
}
}
int main(int argc, char** argv)
{
std::string host = "localhost";
/*
run command to see result
./test 192.168.1.200
*/
if (argc < 2)
{
host = "localhost";
}
else
host = argv[1];
auto &sqlmgr = Mysqlmgr::getinstance();
if (!sqlmgr.connect(host.c_str(), "root", "123", "goodboy", 0, 0, 0))
{
printf("can not connect to mysql: %s\n", sqlmgr.geterror());
return -1;
}
printf("connected sucess!\n");
drop();
create();
insert();
query();
}