poco 是c++ 一个比较好的库,现在正在学习使用它,碰到一些问题记录在此。

poco版本:poco-1.46-all ,带有数据库的支持模块

操作系统:ubuntu

1.使用poco的MySQL模块连接数据库,中文出现乱码

使用的例子是/poco-1.46-all/Data/samples/sqlite改的MySQL

mysql服务器的test数据库,全部是utf-8格式(数据库是8,表也是utf8),但是使用poco连接后,带中文的字段出现乱码

在命令行中输入

man mysql

在 MYSQL OPTIONS 节,看见如下设置

--default-character-set=charset_name

           Use charset_name as the default character set for the client and connection.

           A common issue that can occur when the operating system uses utf8 or another multi-byte character set is
that output from the mysql client is formatted incorrectly, due to the fact that the MySQL client uses the
latin1 character set by default. You can usually fix such issues by using this option to force the client
to use the system character set instead. See Section 10.5, “Character Set Configuration”, for more information.

关键就是这里,default-character-set对应的编码要和数据库的test库的编码一致

也就是说default-character-set设置为utf8才对

怎么在poco的mysql模块设置这个编码呢

看poco中的连接mysql代码如下:

    // register MySQL connector
Poco::Data::MySQL::Connector::registerConnector(); // create a session
Session session(SessionFactory::instance().create("MySQL", "user=root;password=888;db=test"));

很自然的先去SessionFactory.cpp看是否有这个设置(Data/src/SessionFactory.cpp),没发现线索。

再去(Data/MySQL/src/)下面看看

先看Connector.cpp这个文件,发现一点点线索

Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString)
{
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString));
}

就是SessionImpl这个类型,这是真正的实现类

打开同目录下SessionImpl.cpp文件,发现这个类的构造函数中包含对connectionString的解析

std::map<std::string, std::string> options;

解析的结果全部放入上述的options中,

再朝下看,发现

//
// Options
// if (options["compress"] == "true")
{
_mysql.options(MYSQL_OPT_COMPRESS);
}
else if (options["compress"] == "false")
{
// do nothing
}
else if (options["compress"] != "")
{
throw MySQLException("create session: specify correct compress option (true or false) or skip it");
}

还记得最开始的命令行 man mysql不,在 MYSQL OPTIONS 节其中就有一个compress的参数

_mysql.options(MYSQL_OPT_COMPRESS);

上述代码描述了怎么设置compress参数,看一下_mysql的类型

(Data/MySQL/include/Poco/Data/MySQL/sessionImpl.h)

SessionHandle _mysql;

是SessionHandle,然后查看(Data/MySQL/src/),SessionHandle.cpp(或SessionHandle.h)

void SessionHandle::options(mysql_option opt)
{
int res = mysql_options(h, opt, ); if (res != )
{
throw ConnectionException("mysql_options error", h);
}
} void SessionHandle::options(mysql_option opt, bool b)
{
my_bool tmp = b;
int res = mysql_options(h, opt, &tmp); if (res != )
{
throw ConnectionException("mysql_options error", h);
}
}

这里就是设置mysql options参数的地方,看见关键函数mysql_options

去google上查一下 mysql_options

这个http://dev.mysql.com/doc/refman/5.1/en/mysql-options.html

其中发现

MYSQL_OPT_COMPRESS (argument: not used) 
 MYSQL_SET_CHARSET_NAME (argument type: char *)

The name of the character set to use as the default character set. 

很明显现在我需要设置的 MYSQL_SET_CHARSET_NAME这个参数,它是个字符串类型

前面有个 compress的参数,它是bool的

找遍SessionHandle.cpp (.h)只有两个重载

void SessionHandle::options(mysql_option opt)

void SessionHandle::options(mysql_option opt, bool b)

没有设置字符串类型的,那么我们自己动手写了

继续看 http://dev.mysql.com/doc/refman/5.1/en/mysql-options.html

在最下面有例子

MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_OPT_COMPRESS,);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"odbc");
mysql_options(&mysql,MYSQL_INIT_COMMAND,"SET autocommit=0");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",,NULL,))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"odbc");这行就是设置字符串的

现在需要在SessionHandel.h增加一个函数声明
void options(mysql_option opt, char *c);
同时在SessionHandel.cpp实现
void SessionHandle::options(mysql_option opt, char *c)
{ int res = mysql_options(h, opt, c); if (res != )
{
throw ConnectionException("mysql_options error", h);
}
}

在SessionImpl.cpp文件的构造函数,最后如下

//
// Real connect
//
字符串之前增加如下语句
if (options["default-character-set"] != "")
{
_mysql.options(MYSQL_SET_CHARSET_NAME, options["default-character-set"].c_str());
}

最后,把开始的poco中的连接mysql代码改成如下
    // register MySQL connector
Poco::Data::MySQL::Connector::registerConnector(); // create a session
//Session session("user=root;password=888;db=test");
Session session(SessionFactory::instance().create("MySQL", "user=root;password=888;db=test;default-character-set=utf8"));

编译,并查看一下,中文不再乱码了,^_^

使用相同的方法,可以给poco的mysql添加其它设置支持。

c++ poco 使用mysql中文乱码问题的更多相关文章

  1. 解决springmvc+mybatis+mysql中文乱码问题【转】

    这篇文章主要介绍了解决java中springmvc+mybatis+mysql中文乱码问题的相关资料,需要的朋友可以参考下 近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文 ...

  2. 总结--解决 mysql 中文乱码

    首先分析一下导致mysql 中文乱码的原因: 1.建表时使用了latin 编码 2.连接数据库的编码没有指定 3.写入时就已经乱码(这种情况需要自己检查源数据了) 解决方法总结: 1.创建库时指定编码 ...

  3. Servlet、MySQL中文乱码

    1.Servlet中文乱码: 在doPost或doGet方法里,加上以下两行即可: response.setContentType("text/html;charset=UTF-8" ...

  4. php mysql 中文乱码解决方法

    本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...

  5. windows mysql 中文乱码和中文录入提示太大错误的解决方法

    今天操作mysql的时候很郁闷,因为修改默认字符集搞了半天,终于弄成了(关于如何把windows的默认字符集设置成功,可以参看另一篇博文,最终在mysql中输入show variables like ...

  6. MySQL编程(0) - Mysql中文乱码问题解决方案

    MySQL 5.6 for Windows 解压缩版配置安装: http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL ...

  7. MySQL及navicat for mysql中文乱码

    转载自:https://www.cnblogs.com/mufire/p/6697994.html 修改完之后记着重启mysql服务,在服务里边重启,即可生效! 全部使用utf8编码 MySQL中文乱 ...

  8. 通过msyql proxy链接mysql中文乱码及session问题

    1.session问题 问题前提:一台机数据库为两个实例,通过不同的socket监听不同端口对外提供服务.不同的站点都访问同一个VIP不同的端口进行访问数据库. 故障现象:一旦有一个站点先用了这个vi ...

  9. 可遇不可求的Question之导入mysql中文乱码解决方法篇

    可遇不可求的Question之导入mysql中文乱码解决方法篇 先 set names utf8;然后 source c:\1.sql ?

随机推荐

  1. JavaScript设计模式学习之路——继承

    早在学习java的时候,就已经接触了继承,在java中因为有extends关键字,因此继承就比较简单.但是在JavaScript中,只能通过灵活的办法实现类的继承. 下面是我昨天在学习过程中,了解到的 ...

  2. 利用Docker安装Web前端性能测试工具Sitespeed.io

    目录结构 一.Sitespeed.io概述 1.Sitespeed.io简介 2.Sitespeed.io使用场景 二.Sitespeed.io的安装和使用 1.安装Sitespeed.io 2.连接 ...

  3. Hibernate 中 load() 和 get() 的区别

    get 和 load 方式都是是根据 id 取得一个记录.下边详细说一下 get 和 load 的不同,因为有些时候为了对比也会把 find 加进来. 1.从返回结果上对比: load 方式检索不到的 ...

  4. CentOS 7 开放3306端口访问

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.1.关闭firewall:systemctl stop firewalld.servicesystemctl ...

  5. J2EE十三种技术规范介绍

    J2EE的十三个技术规范 J2EE体系结构 一.JDBC:Java Data Base Connectivity,数据库连接 我们大家对微软公司的ODBC数据库访问接口比较熟悉,而在Java中创建数据 ...

  6. CF697D-Puzzles

    题目 一棵树,从根节点开始dfs,每层以随机顺序进入每个子节点,问走到每个点的时候期望经过了多少个点. (这里经过多少个点指的是经过多少个不同的点,即经过一个点多次算一个) (其实这个题不如说求期望d ...

  7. 【bzoj2906】颜色 分块

    题目描述 给定一个长度为N的颜色序列C,对于该序列中的任意一个元素Ci,都有1<=Ci<=M.对于一种颜色ColorK来说,区间[L,R]内的权值定义为这种颜色在该区间中出现的次数的平方, ...

  8. Eve-NG-Toolkit

    Eve-NG-Toolkit 来源 http://www.emulatedlab.com/archives/694 参考 http://eve-ng.cn/doku.php   http://foru ...

  9. servlet中doGet()和doPost()的区别

    1.生成方式 get方法有四种: ①直接在URL地址栏中输入URL ②网页中的超链接 ③form中method为get ④form中method为空时,默认是get提交 post只知道有一种:form ...

  10. 【BZOJ3203】保护出题人(动态规划,斜率优化)

    [BZOJ3203]保护出题人(动态规划,斜率优化) 题面 BZOJ 洛谷 题解 在最优情况下,肯定是存在某只僵尸在到达重点的那一瞬间将其打死 我们现在知道了每只僵尸到达终点的时间,因为僵尸要依次打死 ...