-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_conn.cc
57 lines (49 loc) · 1.44 KB
/
mysql_conn.cc
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
/*
* Copyright (c) 2017-2018 SYZ
*
* https://github.com/mysql/mysql-connector-cpp.git
*
* g++ mysql_conn.cc -std=c++11 -I$MYSQLCPPCONN_HOME/include
* -L$MYSQLCPPCONN_HOME/lib -lmysqlcppconn -o release/mysql_conn
*
*/
#include <cppconn/connection.h>
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_connection.h>
int main(int argc, char* argv[]) {
std::string host = "127.0.0.1:3306";
std::string user = "root";
std::string pswd = "123456";
std::string schema = "sys";
std::string sqlstmt = "show databases";
sql::Driver* pdriver_ = nullptr;
sql::Connection* pconnection_ = nullptr;
sql::Statement* pstmt_ = nullptr;
sql::ResultSet* pres_ = nullptr;
do {
pdriver_ = get_driver_instance();
if (pdriver_ == nullptr) break;
try {
pconnection_ =
pdriver_->connect(host.c_str(), user.c_str(), pswd.c_str());
if (pconnection_ == nullptr) break;
pconnection_->setSchema(schema.c_str());
pstmt_ = pconnection_->createStatement();
if (pstmt_ == nullptr) break;
pres_ = pstmt_->executeQuery(sqlstmt.c_str());
if (pres_ == nullptr) break;
while (pres_->next()) {
std::cout << pres_->getString("Database") << std::endl;
}
} catch (sql::SQLException& e) {
std::cout << e.what() << std::endl;
}
} while (0);
if (pconnection_) {
delete pconnection_;
pconnection_ = nullptr;
}
return 0;
}