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 ...
随机推荐
- [VB.NET Tips]为VB.NET正名
前言 我于2005年毕业,正值全国上上下下如火如荼的开展企业信息化的时代,正是大规模软件开发的年代. 那时.NET 已经发布了2.0,但是仍是VB6,Delphi,PowerBuilder的天下,是E ...
- ASP.NET Core 3.0 使用gRPC
一.简介 gRPC 是一个由Google开源的,跨语言的,高性能的远程过程调用(RPC)框架. gRPC使客户端和服务端应用程序可以透明地进行通信,并简化了连接系统的构建.它使用HTTP/2作为通信协 ...
- [Linux] CentOS安装GNOME时,fwupdate-efi-12-5.el7.centos.x86_64 conflicts with grub2-common-1:2.02-0.65.el7.centos.noarch
参考文章:https://createdpro.com/a/100006 该问题源于文件的版本冲突: grub2-common包的冲突,所以要将该包使用yum update grub2-commonn ...
- jenkins自动化部署项目6 --STMP服务发送邮件配置
[系统管理]-[系统设置]里 1.系统管理员邮件地址:和后面SMTP配置认证的邮箱要一致,即发件箱 --------切记 2.配置SMTP认证的前提是需要在sohu搜狐邮箱开通SMTP/POP3, ...
- 常用的HDFS操作
首先,把Hadoop命令加入到PATH环境变量中,直接通过start-dfs.sh开启Hadoop,也可以直接通过hdfs命令访问HDFS中的内容,方便平时的操作. 配置PATH环境变量 vim ~ ...
- Scala Data Structure
Arrays Array 固定长度:ArrayBuffer 可变长度 arr.toBuffer, buf.toArray 初始化是不要使用 new 使用 () 访问元素 使用 for (elem &l ...
- 安装pytest-allure-adaptor后,运行报错:AttributeError: module 'pytest' has no attribute 'allure'
原因:因为pytest-allure-adaptor库基本被python3放弃了,运行很不友好,反正我运行就是报错 解决方法: 先卸载:pip uninstall pytest-allure-ad ...
- java后端研发经典面试题总结
垃圾回收算法 1.标记-清除算法 标记-清除算法是最基本的算法,和他的名字一样,分为两个步骤,一个步骤是标记需要回收的对象.在标记完成后统一回收被标记的对象.这个算法两个问题.一个是效率问题,标记和清 ...
- 机器学习回顾篇(6):KNN算法
1 引言 本文将从算法原理出发,展开介绍KNN算法,并结合机器学习中常用的Iris数据集通过代码实例演示KNN算法用法和实现. 2 算法原理 KNN(kNN,k-NearestNeighbor)算法, ...
- php无限级分类实战——评论及回复功能
经常在各大论坛或新闻板块详情页面下边看到评论功能,当然不单单是直接发表评论内容那么简单,可以对别人的评论进行回复,别人又可以对你的回复再次评论或回复,如此反复,理论上可以说是没有休止,从技术角度分析很 ...