MySQL Connector/C++ 1.1.5 宣告,此版本的二进制版本需要运用 Boost 1.54.0 编译。
凭证自己零星平台下载响应的版本。文件夹名字过长,将“mysql-connector-c++-noinstall-1.0.5-win32”改为“mysql”。
MySQL Connector/ODBC配置装备部署措施:
下面要配置装备部署vs2008的情景。
1. 名目属性页->C/C++->General->Additional Include Directories。将mysqlinclude目录削减进去。
2. 名目属性页->Linker->General->Additional Library Directories。将mysqllib与$MySQLbin目录削减进去。
3. 名目属性页->Linker->Input->Additional Dependencies。削减这两项mysqlcppconn.lib,mysqlcppconn-static.lib(mysqllib目录下的两个.lib文件)
4. 将mysqllib下的mysqlcppconn.dll文件与$MySQLbinlibmySQL.dll复制到windowssystem32文件夹下。
情景配置装备部署竣事。
数据库运用:
在衔接数据库以前,先建树一张表。 (着实这些可能在代码中实现,我这样是为了让测试代码尽可能精练易查错)
掀开操作台,输入mysql -u root -p,输入明码。
魔难之后已经有的数据库。(SQL语句开始加之';'展现赶快实施之后语句。)
mysql> show databases;
建树数据库
mysql> create database test;
运用数据库(这句不能加分号)
mysql> use test
魔难已经有的表
mysql> show tables;
建树表
mysql> create table testuser ( id INT, name CHAR(20));
插入数据
mysql> insert into testuser(id, name) values(1001, 'google');
mysql> insert into testuser(id, name) values(1002, 'kingsoft');
mysql> insert into testuser(id, name) values(1003, 'firefox');
如今在C++中查问这些数据
#include "stdafx.h"
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <statement.h>
using namespace sql;
using namespace std;
void RunConnectMySQL()
{
mysql::MySQL_Driver *driver;
Connection *con;
Statement *state;
ResultSet *result;
// 初始化驱动
driver = sql::mysql::get_mysql_driver_instance();
// 建树链接
con = driver->connect("tcp://127.0.0.1:3306", "root", "123");
state = con->createStatement();
state->execute("use test");
// 查问
result = state->executeQuery("select * from testuser where id < 1002");
// 输入查问
while(result->next())
{
int id = result->getInt("ID");
string name = result->getString("name");
cout << id << " : " << name << endl;
}
delete state;
delete con;
}
int _tmain(int argc, _TCHAR* argv[])
{
RunConnectMySQL();
getchar();
return 0;
}
接口实例:
bool Co妹妹onService::......(JSONNode& in, JSONNode& out)
{
/*=====校验json传入参数=====*/
..........
/*=====剖析json传入参数,患上到装置码概况id, 装置光阴, 硬盘序列号, ip,mac=====*/
..........
/*=====经由装置码找到数据库中的对于应记实写入传入的值=====*/
/********数据库操作**********/
//1.患上到数据库衔接
Connection* con = G<ConnectionPool>().GetConnection();
if (con == NULL)
{
LOG4CXX_ERROR(g_logger, "不能患上到数据库衔接");
out.push_back(JSONNode(RESULT, ACTION_FALSE));
out.push_back(JSONNode(ERROR_MESSAGE, "不能患上到数据库衔接"));
return false;
}
int ret = 1;
PreparedStatement* prep_stmt = NULL;
ResultSet* res = NULL;
try
{
con->setSchema(G<ConnectionPool>().GetDBName().c_str());
//实施sql修正装置形态
std::string sql_statement = "update tb_host set reg_date=?, sn=?, ip=?, mac=?, state=?, sync_state=? where reg_code =? and state=?";//要实施的sql语句
//事件处置
con->setAutoCo妹妹it(0);
prep_stmt = con->prepareStatement(sql_statement.c_str());
prep_stmt->setString(1, install_time.c_str());
prep_stmt->setString(2, harddrive_sn.c_str());
prep_stmt->setString(3, ip_address.c_str());
prep_stmt->setString(4, mac_address.c_str());
prep_stmt->setInt(5, HAS_INSTALL);
prep_stmt->setInt(6, HAS_SYNC);
prep_stmt->setString(7, install_code.c_str());
prep_stmt->setInt(8, NO_INSTALL);
if(prep_stmt->executeUpdate() == 0)
{
ret = 2;
LOG4CXX_INFO(g_logger, ".....");
out.push_back(JSONNode(ERROR_MESSAGE, "....."));
goto Finally_handle;
}
//调用赋默认策略存储历程
std::string procedure = "CALL updateHostPolicyByModHost(?,?, @ret, @msg)";
prep_stmt = con->prepareStatement(procedure.c_str());
prep_stmt->setString(1, install_code.c_str());
prep_stmt->setInt(2, 0);
prep_stmt->execute();
std::string query = "select @ret AS ret,@msg AS msg";
prep_stmt = con->prepareStatement(query.c_str());
res = prep_stmt->executeQuery();
while(res->next())
{
if(res->getInt("ret") != 0)
{
LOG4CXX_ERROR(g_logger, "....." << res->getString("msg").c_str() << res->getInt("ret"));
out.push_back(JSONNode(ERROR_MESSAGE, "....."));
goto Finally_handle;
}
}
con ->co妹妹it();
}
catch (SQLException& e)
{
try
{
con->rollback();
}
catch (SQLException& e)
{
ret = 0;
LOG4CXX_ERROR(g_logger, "数据库颇为" << e.what());
goto Finally_handle;
}
ret = 0;
LOG4CXX_ERROR(g_logger, "数据库颇为" << e.what());
out.push_back(JSONNode(ERROR_MESSAGE, e.what()));
goto Finally_handle;
}
catch (...)
{
ret = 0;
LOG4CXX_ERROR(g_logger, "其余过错");
out.push_back(JSONNode(ERROR_MESSAGE, "其余过错"));
goto Finally_handle;
}
Finally_handle:
DestorySql(res, prep_stmt);
//将衔接释放到衔接池
G<ConnectionPool>().ReleaseConnection(con);
if (ret == 1)
{
out.push_back(JSONNode(RESULT, ACTION_SUCCESS));
return true;
}
else if (ret == 2)
{
out.push_back(JSONNode(RESULT, ACTION_FALSE));
return true;
}
else
{
out.push_back(JSONNode(RESULT, ACTION_FALSE));
return false;
}
}
/************************************************************************/
/* 销毁数据库记实集资源 */
/************************************************************************/
void Co妹妹onService::DestorySql(ResultSet* res, PreparedStatement* prep_stmt)
{
if (res != NULL)
{
try
{
res ->close();
}
catch(SQLException& e)
{
LOG4CXX_ERROR(g_logger, "数据库颇为" << e.what());
}
delete res;
res = NULL;
}
if (prep_stmt != NULL) {
try
{
prep_stmt->close();
}
catch(SQLException& e)
{
LOG4CXX_ERROR(g_logger, "数据库颇为" << e.what());
}
delete prep_stmt;
prep_stmt = NULL;
}
}
更新:
新增的特色以及改善
Connector/C++ 反对于如下的衔接选项:sslVerify (boolean),sslCRL (string) 以及sslCRLPath (string)。(Bug #18461451)
Connector/C++ 新增函数提供方式,表以及列的字符集以及服从集的校对于元数据 :(Bug #72698, Bug #18803345)
ResultSet * DatabaseMetaData::getSchemaCollation(const sql::SQLString& catalog, const sql::SQLString&schemaPattern)
ResultSet * DatabaseMetaData::getSchemaCharset(const sql::SQLString& catalog, const sql::SQLString& schemaPattern)
ResultSet * DatabaseMetaData::getTableCollation(const sql::SQLString& catalog, const sql::SQLString&schemaPattern,const sql::SQLString&tableNamePattern)
ResultSet * DatabaseMetaData::getTableCharset(const sql::SQLString& catalog, const sql::SQLString&schemaPattern, const sql::SQLString&tableNamePattern)
SQLString ResultSetMetaData::getColumnCollation(unsigned int columnIndex)
SQLString ResultSetMetaData::getColumnCharset(unsigned int columnIndex)
Connector/C++ 反对于 MYSQL_OPT_CONNECT_ATTR_ADD 选项,能接管一个 std::map 参数。(Bug #72697, Bug #18803313)
Connector/C++ 反对于一个 useLegacyAuth 衔接选项 (Bug #69492, Bug #16970753)
Connector/C++ 编译以及链接到 Connector/C 6.1.5 而不是 libmysql
Bugs 修复
MySQL_ResultSetMetaData::getColumnTypeName()returned UNKNOWN for LONG_BLOB fields. (Bug #72700, Bug #18803414)
Connector/C++ version-information methods have been revised to return the correct values. (Bug #66975, Bug #14680878)
Definitions for character sets and collations were added (utf8mb4 in particular). (Bug #71606, Bug #18193771)