、安装好boost。
、从官网下载mysql connector c++版本。
、解压,复制 include/jdbc/cppconn 文件夹复制,到/usr/local/include/cppconn目录。,其他.h文件到到/usr/local/include/。 4、复制lib64中库文件到/usr/local/lib/目录中。
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <stdio.h>
using namespace std; int main(void)
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
try {
driver = get_driver_instance();
con = driver->connect("tcp://127.00.1:3306", "root", "command");
con->setSchema("viewfocus");
}
catch (sql::SQLException &e)
{
printf("error \n");
}
return ;
}
SRC += main.cpp
GCC = g++
FLAG = -lmysqlcppconn
CMS = CMS demo:
$(GCC) -o $(CMS) -g $(SRC) $(FLAG)
clean:
rm CMS
CMysqlConnectPtr mysqlMgr::CreateConnection()
{
db_conf db = config::get_db_conf(); bool b_true = true;
char strHonst[] = { '\0' };
int nTime_out = ;
snprintf(strHonst, sizeof(strHonst), "tcp://%s:%s", db.strDBIP.c_str(), db.strDBPort.c_str());
spd::get("console")->info("{}", strHonst);
sql::Connection *conn = driver->connect(strHonst, db.strDBUser.c_str(), db.strDBPswd.c_str());
conn->setClientOption("OPT_CONNECT_TIMEOUT", &nTime_out);
conn->setClientOption("OPT_RECONNECT", &b_true);
conn->setClientOption("CLIENT_MULTI_RESULTS", &b_true);
conn->setClientOption("OPT_CHARSET_NAME", "utf8");
conn->setSchema(db.strDBName.c_str());
std::shared_ptr<sql::Connection> sp(conn,[](sql::Connection *conn) {
delete conn;
}); return sp;
}
    //*************************************************************************
// 函数名称: SyncExecSQL
// 返 回 值: bool --执行成功返回true, 否则返回false
// 参 数: FUNCCALL fun --可以是回调函数,仿函数,lambda表达式
// 函数说明: 同步执行一个数据库操作,
//*************************************************************************
template<class FUNCCALL>
bool SyncExecSQL(FUNCCALL fun)
{
bool bResult = false; CMysqlConnectPtr pDB = CreateConnection();
if (!pDB)
{
spd::get("console")->info("{} {} {}", __FILE__, __LINE__,"SyncExecSQL(FUNCCALL fun)");
return bResult;
}
try
{
fun(pDB);
bResult = true;
}
catch (sql::SQLException &e)
{
spd::get("console")->info("{} {} {}", __FILE__, __LINE__, "sql::SQLException");
}
catch (...)
{
}
return true;
}
void mysqlMgr::loadMonitor()
{
cleanMap(); string strSql = "SELECT Dev_Code, Video_Alarm_Type from m_monitor "; SyncExecSQL([&](CMysqlConnectPtr pDB)
{
try {
std::unique_ptr<sql::PreparedStatement> pstmt(pDB->prepareStatement(strSql));
std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
if (res)
{
while (res->next())
{
int index = MYSQL_BASE_INDEX_1; string devCode = res->getString(++index);
string alamType = res->getString(++index); add2Map(devCode, alamType);
}
}
}
catch (sql::SQLException &e)
{
return false;
}
return true;
});
}

Linux::mysql-connector-c++的更多相关文章

  1. [转]MySQL Connector/C++(一)

    http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...

  2. MySQL Connector/C++ C++连接mysql

    MySQL :: MySQL Connector/C++ Developer Guide :: 1 Introduction to Connector/C++ https://dev.mysql.co ...

  3. MySQL、Hive以及MySQL Connector/J安装过程

    MySQL安装 ①官网下载mysql-server(yum安装) wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch. ...

  4. 创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL

    创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL 用惯.NET的研发人员都习惯性地使用SQLServer作为数据库.然而.NET Core ...

  5. vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错

    包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...

  6. Using MySQL Connector .NET 6.6.4 with Entity Framework 5

    I had been waiting for the latest MySQL connector for .NET to come out so I can move on to the new a ...

  7. linux mysql远程连接

    今天在本地连接linux服务端的mysql始终报错61,谷歌后找到原因: linux的mysql默认是不允许远程连接操作的,在stack上面找到方法:修改mysql配置文件/usr/local/mys ...

  8. mysql.connector操作mysql的blob值

    This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...

  9. ubuntu linux mysql 安装 基本操作 命令

    mysql --help #如果有信息证明系统已经安装了mysql mysql -V #查看版本号 netstat -tap|grep mysql #检查mysql是否在启动状态 卸载mysql: s ...

  10. Ubuntu & MacOS安装Mysql & connector

    Ubuntu & MacOS安装Mysql & connector 1. 安装MySql sudo apt-get install mysql-server apt-get insta ...

随机推荐

  1. 纯css耍个透明正方体转一转

    效果 效果图如下 ​ 实现思路 定义一个最外层的容器,用来控制显示的位置 定义一个父容器,用来设置元素被查看位置的视图,这里使用到CSS3的perspective 属性 定义子容器,设置为相对定位,利 ...

  2. 第一次作业:使用Packet Tracer分析HTTP包

    0 个人信息 张樱姿 201821121038 计算1812 1 实验目的 熟练使用Packet Tracer工具.分析抓到的HTTP数据包,深入理解:HTTP协议,包括语法.语义.时序. 2 实验内 ...

  3. 使用PrepareStatement

    包结构: 第一步:编写获取连接工具类 package com.atguigu.jdbc; import java.io.IOException; import java.io.InputStream; ...

  4. BOM之window核心模块

    Window对象包含以下五大核心:document,screen,navigator,history,location. 一     document 文档 document包含了浏览器对标准DOM实 ...

  5. 微信小程序开发简述

    微信小程序简述 什么是微信小程序? 微信小程序,简称小程序,英文名Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用.全面开放 ...

  6. 列表 元祖 range

    1.列表 list 存放一些数据的容器 比如 衣柜 书包 作用:存储一些数据,数据量比较大 可以下标 可以切片 可以步长 和字符串的完全一样 lst = [1,2,3] print(lst) #[1, ...

  7. jq 加载的几种方法

    jQuery加载的几种方式 //①页面加载完之前执行,与嵌入的js加载方式一样 (function ($) {     alert('start'); })(jquery); //②页面加载后执行 $ ...

  8. [Python] Python 学习记录(1)

    1.概论 弱类型 一个变量能被赋值时能与原类型不同 x = 1 x = "1"  #不加分号,给x赋值为int后再次赋值为string是可行的 与或非 and or not /  ...

  9. Linux下beego及beego相关插件安装

    Linux下beego及beego相关插件安装 1.下载及配置go环境看见链接: http://golang.org/dl/ 网盘:链接: https://pan.baidu.com/s/1MveUM ...

  10. java不支持多继承

    java不支持多继承,但支持多重继承,即一个类只能继承一个类,使用extends关键字 一个类可以实现多个接口,接口之间用逗号分隔,使用implements关键字 一个接口可以继承多个其他接口,接口之 ...