关于c++连接mysql
使用Mysql connector c++
目前尽在x64 release下生效
// CardGame.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#include "BaseInc.h"
//#include <map>
//#include "TcpClient.h"
//
//GS g;
//TcpClient client;
//#include <sstream>
//int main()
//{
// client.Init("47.95.38.221", 12000);
// client.StartReceiveThread();
//// client.Receive()
// /*std::stringstream ss;
// ss << "abc\r\ndd\r\naas";
// string line;*/
// //std::map<std::string, std::stringstream> mps;
// //{
// // mps["aa"] = std::stringstream();
// //}
// // mps["aa"] << "lina";
// //while (!ss.eof()) {
// // std::getline(ss, line);
// // cout << line << endl;
// // if (line.rfind("\r") == line.size() - 1) {
// // cout << "\rline" << endl;
// // }
// // else {
// // cout << "not \r" << endl;
// // ss.clear();
// // ss << line;
// // break;
// // //ss.write(line.data(), line.size());
// // }
// //}
//// line.clear();
// //while (!ss.eof()) {
// // std::getline(ss, line);
// // ss >> line;
// //}
// //ss.clear();
// //ss.str("aaa");// << "aa bb cc aa" << endl;
// //while (!ss.eof()) {
// // std::getline(ss, line);
// // ss >> line;
// //}
// //ss << "1 ";
// //ss >> line;
// //cout << line << endl;
// //cout <<ss.str() <<","<< line << endl;
// //ss >> line;
// //cout <<ss.str() <<","<< line << endl;
// //ss << "2 ";
// //ss >> line;
// //cout << line << endl;
// //ss << "3 ";
// //ss >> line;
// //cout << line << endl;
// //ss >> line;
// //cout << line << endl;
// //ss >> line;
// //cout << line << endl;
// //ss >> line;
// //cout << ss.str() << "," << line << endl;
//
// std::string str = "0-4|1-2|2-3";
// istringstream iss(str);
// std::string c;
// int s, p;
// while (!iss.eof())
// {
// std::getline(iss, c, '|');
// cout << c << endl;
// sscanf_s(c.data(), "%d-%d", &s, &p);
// cout << "s,p" << s << "," << p << endl;
// }
//
// return 0;
//}
#include <iostream>
#include "mysql/jdbc.h"
#include "jdbc/mysql_driver.h"
#include "jdbc/mysql_connection.h"
#pragma comment(lib,"mysqlcppconn.lib")
#pragma comment(lib,"mysqlcppconnx.lib")
#pragma comment(lib,"libcrypto.lib")
using namespace sql;
using namespace mysql;
using namespace std;
Connection* conn;
Driver* driver;
sql::Statement* stmt;
int main() {
//sql_mysql_get_driver_instance();
//driver = sql::mysql::get_mysql_driver_instance();
MySQL_Driver* d = reinterpret_cast<MySQL_Driver*>(sql_mysql_get_driver_instance());
std::string host = "tcp://127.0.0.1:3306";
std::string usr = "root";
std::string pwd = "1234";
d->threadInit();
cout << d->getName().asStdString() << endl;
// MySQL_Connection* msc = dynamic_cast<>():
conn = d->connect(host, usr, pwd);//("127.0.0.1", "root", "1234");
//std::unique_ptr<sql::Connection> con(driver->connect(host, usr, pwd));
cout << "connect" << endl;
stmt = conn->createStatement();
stmt->execute("USE " "dbtest");
stmt->execute("DROP TABLE IF EXISTS test23");
stmt->execute("CREATE TABLE test23(id INT, label CHAR(1))");
stmt->execute("INSERT INTO test23(id, label) VALUES (1, 'a')");
delete stmt;
conn->close();
return 0;
}
浙公网安备 33010602011771号