您的位置:首页 > 编程语言 > C语言/C++

编译和使用 MySQL C++ Connector

2013-08-12 12:21 519 查看
记录编译 mysql C and C++ connector 和简单访问数据库.

环境: vs2012, mysql 5.6.13, 基于x64

0. 软件包

mysql http://dev.mysql.com/downloads/mysql/

c connector http://dev.mysql.com/downloads/connector/c/

c++ connector依赖C connector, mysql的binary里面有编译好的C connector lib, 如果你用的编译器和mysql 一样, 那么不用重新编译

c++ connector http://dev.mysql.com/downloads/connector/cpp/

boost http://sourceforge.net/projects/boost/files/boost/1.54.0/

c++ Connector 依赖boost. 本来还尝试过vs2013, 但是boost1.54在vs2013 preview下编译有问题, 我没有试低版本的boost.

cmake http://www.cmake.org/cmake/resources/software.html

mysql的project是基于cmake. cmake可以生成vc project, 使用起来挺方便

1. 环境bat vs2012x64env.bat

call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" amd64

set PATH=%PATH%;D:\mybin\cmake-2.8.11.1-win32-x86\bin

set MYSQL_DIR=D:\mybin\mysql-5.6.13-winx64

set BOOST_ROOT=D:\mybin\boost_1_54_0

cmd /K

2. Generate vc project



C connector

cmake -G "Visual Studio 11 Win64"

build project libmysql, 只需要一个lib, 不用编译所有, debug模式下, 自带trace

C++ connector

如果想要打开trace, 加-DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1, 实际还没有结束, 大概是cmake生成project的错误, 需要手动添加预定义宏 CPPCONN_TRACE_ENABLED

cmake -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1 -G "Visual Studio 11 Win64"

build project mysqlcppconn

把头文件copy在mysql下, 方便些, 不是必须

copy D:\mybin\mysql-connector-c++-1.1.3\cppconn D:\mybin\mysql-5.6.13-winx64\include\cppconn

3.Demo, 贴代码了

#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", ""));

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";

}

4. 如果想导入sample数据库
http://dev.mysql.com/doc/index-other.html
download world database InnoDB version

mysql -u root

create database world;

use world;

source c:\world_innodb.sql

show tables;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: