Linux::mysql-connector-c++
、安装好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++的更多相关文章
- [转]MySQL Connector/C++(一)
http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...
- MySQL Connector/C++ C++连接mysql
MySQL :: MySQL Connector/C++ Developer Guide :: 1 Introduction to Connector/C++ https://dev.mysql.co ...
- MySQL、Hive以及MySQL Connector/J安装过程
MySQL安装 ①官网下载mysql-server(yum安装) wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch. ...
- 创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL
创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL 用惯.NET的研发人员都习惯性地使用SQLServer作为数据库.然而.NET Core ...
- vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错
包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...
- 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 ...
- linux mysql远程连接
今天在本地连接linux服务端的mysql始终报错61,谷歌后找到原因: linux的mysql默认是不允许远程连接操作的,在stack上面找到方法:修改mysql配置文件/usr/local/mys ...
- mysql.connector操作mysql的blob值
This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...
- ubuntu linux mysql 安装 基本操作 命令
mysql --help #如果有信息证明系统已经安装了mysql mysql -V #查看版本号 netstat -tap|grep mysql #检查mysql是否在启动状态 卸载mysql: s ...
- Ubuntu & MacOS安装Mysql & connector
Ubuntu & MacOS安装Mysql & connector 1. 安装MySql sudo apt-get install mysql-server apt-get insta ...
随机推荐
- Python3 爬虫之 Scrapy 框架安装配置(一)
博客地址:http://www.moonxy.com 基于 Python 3.6.2 的 Scrapy 爬虫框架使用,Scrapy 的爬虫实现过程请参照本人的另一篇博客:Python3 爬虫之 Scr ...
- 60 (OC)* 23中设计模式
git设计模式
- .netCore部署在IIS上遇到的问题(500.19,500.21错误)
1.确保IIS功能都安装上了. 2.确保.netcore 的最新sdk已安装. 3.应用程序池改成无托管代码 4.500.19错误 错误原因,没有安装 DotNetCore.2.0.5-Windows ...
- 将SpringBoot部署在外部tomcat中
一,前言 在文章SpringBoot之简单入门中提到了,SpringBoot是内置一个tomcat容器的,但是如果要将SpringBoot部署在一个外部的tomcat,要怎么办呢?这就是本篇文章的目的 ...
- C#之WPF连接sqllite数据库
using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; namespa ...
- JavaScript总结(一)
一.JavaScript 简介 1.1 .什么是 JavaScript? JavaScript 的简称:JS. JavaScript 是一个脚本.(不需要经过编译器编译的语言就叫做脚本) JavaSc ...
- phpexcel来做表格导出(多个工作sheet)及设置单元格格式
<?php /** * 简单实用Execl */ set_include_path('.'.get_include_path().PATH_SEPARATOR.dirname(__FILE__) ...
- 夯实Java基础系列19:一文搞懂Java集合类框架,以及常见面试题
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- 实现一个3D图片轮播插件 —— 更新版
前言: 前段时间写下了之前那篇 3D图片轮播效果,后来发现了 Pedro Botelho 写的jquery.gallery.js ,于是重新修改了自己的这个图片轮播,使之可以成为一个插件来使用 ...
- 校园网 虚拟机VMware Linux桥接模式 无法上网 问题
只是解决常见虚拟机桥接模式 无法上网问题,基本的百度都有 基本知识 虚拟机有三种网络连接模式:桥接模式,net模式,仅主机 桥接模式:同一网段允许的话,相当于一个独立的物理主机,独立ip net模式: ...