编译和使用 MySQL C++ Connector
记录编译 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.htmldownload world database InnoDB version
mysql -u root
create database world;
use world;
source c:\world_innodb.sqlshow tables;
编译和使用 MySQL C++ Connector的更多相关文章
- Linux下MySQL c++ connector示例
最近在学习数据库的内容,起先是在windows下用mysql c++ connector进行编程,之所以选用c++而不是c的api,主要是考虑到c++ connector是按照JDBC的api进行实现 ...
- 转载:Centos7 从零编译Nginx+PHP+MySql 二
序言 这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先 ...
- 转载:Centos7 从零编译Nginx+PHP+MySql 序言 一
这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先的一些 ...
- [原创]Centos7 从零编译Nginx+PHP+MySql
序言 这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先 ...
- CentOS下PHP7的编译安装,MySQL的支持和一些问题的解决
最近试了一下PHP7,在编译和支持MySQL上都遇到一些问题,相信不少同学也同样遇到,所以在这里聊一下这个过程.简单来讲编译PHP7只需要3步: 1../buildconf --force 2../c ...
- mysql ODBC connector相关问题
mysql ODBC connector我安装了,怎么就不成功了 进到命令行,运行下边的:C:\>cd \windows\SysWOW64 C:\Windows\SysWOW64>odbc ...
- CentOS 6.0最小化编译安装Nginx+MySQL+PHP+Zend
http://www.osyunwei.com/archives/235.html 引言: 操作系统:CentOS 6.0 32位 下载地址:http://mirrors.163.co ...
- CentOS6.6 32位 Minimal版本纯编译安装Nginx Mysql PHP Memcached
声明:部分编译指令在博客编辑器里好像被处理了,如双横线变成单横线了等等,于是在本地生成了一个pdf版本,在下面地址可以下载. LNMP+Memcached CentOS是红帽发行的免费的稳定Linux ...
- windows下编译qt的mysql驱动
windows下编译qt的mysql驱动cd %QTDIR%\src\plugins\sqldrivers\mysqlqmake –o Makefile INCLUDEPATH+="C:\M ...
随机推荐
- wdcp(WDlinux Control Panel) 快速安装RPM包,lanmp一件安装
lanmp一键安装包是wdlinux官网2010年开始推出的lamp,lnmp,lnamp(apache,nginx,php,mysql,zend,eAccelerator,pureftpd)应用环境 ...
- Codeforces Round #357 (Div. 2) D. Gifts by the List 水题
D. Gifts by the List 题目连接: http://www.codeforces.com/contest/681/problem/D Description Sasha lives i ...
- BZOJ 1191: [HNOI2006]超级英雄Hero 匈牙利算法
1191: [HNOI2006]超级英雄Hero Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx Solved: 2xx 题目连接 http:/ ...
- Python学习笔记(四):字符串的学习
总结的内容: 1.字符串常用的方法 2.Python字符串格式化 3.Python字符串转义字 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
- GIT(3)----问题汇总
1.git pull出现的合并问题: Please enter a commit message to explain why this merge is necessary,especially i ...
- Chrome DevTools Protocol Viewer
Chrome DevTools Protocol Viewer awesome-chrome-devtools
- 通过adb shell操作android真机的SQLite数据库
要通过命令行直接操作android真机上的SQLite数据库,可以直接通过adb shell来完成,不过,前提是必须获得root权限. 另外,android系统其实就是linux的shell,这个应该 ...
- GIT 详解2
https://segmentfault.com/a/1190000000738398 http://www.cnblogs.com/cposture/p/4903767.html https://g ...
- C#中,什么时候用yield return
yield关键字用于遍历循环中,yield return用于返回IEnumerable<T>,yield break用于终止循环遍历. 有这样的一个int类型的集合: static Lis ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...