安装版的mysql connector c++ 不能用来连接,只好重新编译
需要下载mysql-connector-c++-1.1.3,mysql-connector-c-6.1.5-src,boost_1_55_0, cmake
(1):
编译mysql-connector-c-6.1.5-src
cmake -G "Visual Studio 12"
用vs2013编译完成
(2):编译mysql-connector-c++-1.1.3
set MYSQL_DIR=D:\Program Files\MySQL\MySQL Connector C 6.1
set BOOST_ROOT=D:\Program Files (x86)\boost_1_55_0
cmake -G "Visual Studio 12"
用vs2013编译完成
经过上面两部就会生成libmysql.lib、libmysql.dll、mysqlcppconn.dll、mysqlcppconn.lib, mysqlcppconn-static.lib
这个就可以做开发了,最后贴一个测试的列子
#include <iostream>
#include <memory>
#include <string>
#include <cppconn\driver.h>
#include <cppconn\exception.h>
#include <cppconn\resultset.h>
#include <cppconn\statement.h>
#include <cppconn\prepared_statement.h>
static const char* g_sqltrance = "";//"d:t:O,client.trace";
static const int g_enable_debug_trace = 0;
int main()
try
{
std::cout << "connect db start ... \n";
auto driver = get_driver_instance();
std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "123456"));
con->setClientOption("libmysql_debug", g_sqltrance);
con->setClientOption("clientTrace", &g_enable_debug_trace);
con->setSchema("test");
// create table
std::unique_ptr<sql::Statement> stmt(con->createStatement());
stmt->execute("drop table if exists test");
stmt->execute("create table test(id int, label char(1))");
// insert
stmt->execute("insert into test(id, label) values (1, 'a')");
stmt->execute("insert into test(id, label) values (2, 'b')");
stmt->execute("insert into test(id, label) values (3, 'c')");
// update
stmt->executeUpdate("update test set label='e' where id=2");
// prepared statement
std::unique_ptr<sql::PreparedStatement> prep_stmt(con->prepareStatement("insert into test(id, label) values(?, ?)"));
size_t n = 5;
while (n++ < 30)
{
prep_stmt->setInt(1, n);
prep_stmt->setString(2, sql::SQLString(std::string(1, 'A' + n)));
prep_stmt->execute();
}
// query
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery("select id, label from test order by id asc"));
while (res->next())
{
std::cout << "id = " << res->getInt(1) << " label = " << res->getString("label") << "\n";
}
return 0;
}
catch (sql::SQLException & e)
{
std::cout << "SQLException : \n "
<< e.what()
<< "\n ErrorCode " << e.getErrorCode() << " "
<< "\n SQLState " << e.getSQLState() << "\n";
}
catch (...)
{
std::cout << "uncatch exception\n";
}