Skip to content

Commit 9ae1574

Browse files
author
unknown
committed
Modify C + + MySQL example
1 parent 6077fb4 commit 9ae1574

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

example/mysql/mysql_test.cpp

+27-17
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "SqlPrepare.h"
88

99

10-
int _main()
10+
void sql_query_1()
1111
{
12-
DBInterfaceMysql mysql("127.0.0.1", "jw_test", "root", "1111");
12+
DBInterfaceMysql mysql("127.0.0.1", "jw_test", "root", "111111");
1313
mysql.connect();
1414

1515

@@ -25,15 +25,14 @@ int _main()
2525
result >> id;
2626
result >> num;
2727
result >> name;
28-
}
2928

30-
system("pause");
31-
return 0;
29+
printf("id:%d, num:%d, name:%s\n", id, num, name.c_str());
30+
}
3231
}
3332

3433
#include "SqlResultSet.h"
3534

36-
int __main()
35+
void sql_query_2()
3736
{
3837
DBInterfaceMysql mysql("127.0.0.1", "jw_test", "root", "111111");
3938
mysql.connect();
@@ -43,25 +42,22 @@ int __main()
4342
pre.pushInt32(1);
4443
pre.prepare(mysql.mysql());
4544

46-
std::shared_ptr<SqlResultSet> result = std::make_shared<SqlResultSet>();
47-
pre.execute(result.get());
45+
SqlResultSet result;
46+
pre.execute(&result);
4847

49-
while (result->fetch())
48+
while (result.fetch())
5049
{
51-
int id = result->getInt32();
52-
int num = result->getInt32();
53-
std::string name = result->getString();
50+
int id = result.getInt32();
51+
int num = result.getInt32();
52+
std::string name = result.getString();
5453

5554
printf("id:%d, num:%d, name:%s\n", id, num, name.c_str());
5655
}
57-
58-
system("pause");
59-
return 0;
6056
}
6157

6258
#include <functional>
6359
#include "DBThreadPool.h"
64-
int main()
60+
void sql_query_3()
6561
{
6662
DBConfig config;
6763
config.dbname = "jw_test";
@@ -101,7 +97,21 @@ int main()
10197
Sleep(10);
10298
pool.update();
10399
}
100+
}
101+
102+
#include <iostream>
103+
int main()
104+
{
105+
MySQL::libraryInit();
106+
MySQL::threadSafe();
107+
108+
sql_query_1();
109+
std::cout << "**************************************" << std::endl;
110+
sql_query_2();
111+
std::cout << "**************************************" << std::endl;
112+
sql_query_3();
113+
114+
MySQL::libraryEnd();
104115

105-
system("pause");
106116
return 0;
107117
}

0 commit comments

Comments
 (0)