通过 C++ ODB 将中文字符插入 mysql 时出现问题

problem when inserting chinese characters into mysql via C++ ODB

我不会说英语,请容忍我的表达。当我通过odb 向mysql 5.7 中插入一些汉字时,我得到了错误信息"Incorrect string value: '\xD6\xD0\xB9\xFA' for column 'NATION' at row 1",我知道这是字符集的错误设置,我试图解决这个问题但都是徒劳的。

odb 2.4、mysql 5.7、mysql 连接器 c6.1.11 和 visual studio 2015 在我的项目中使用,我已经将 mysql 字符集更改为utf8mb4。下面是我的 mysql 配置文件:

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
[mysql]
default-character-set=utf8mb4
[client]
default-character-set=utf8mb4

而且 mysql 服务器似乎没有问题:

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client     | utf8mb4            |
| character_set_connection | utf8mb4            |
| character_set_database   | utf8mb4            |
| character_set_filesystem | binary             |
| character_set_results    | utf8mb4            |
| character_set_server     | utf8mb4            |
| character_set_system     | utf8               |
| collation_connection     | utf8mb4_general_ci |
| collation_database       | utf8mb4_general_ci |
| collation_server         | utf8mb4_general_ci |
+--------------------------+--------------------+

mysql> show create table basininfo;
------------------------------------------------------------------------------------------------+

    | basininfo | CREATE TABLE `basininfo` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `BASIN_NAME` varchar(1000) DEFAULT NULL,
      `NATION` varchar(1000) DEFAULT NULL,  
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 

我还尝试更改为 mysql 8.0,将 utf8mb4 字符硬编码到 libodb-mysql 库中,如下所示:

mysql_options(handle_, MYSQL_SET_CHARSET_NAME, "utf8mb4");
mysql_options(handle_, MYSQL_INIT_COMMAND, "SET NAMES utf8mb4");

但是这些都不能解决我的问题

导致错误的代码如下:

auto_ptr<odb::database> db(
    new odb::mysql::database(
    "news"     // database login name  
    , "news123" // database password  
    , "newsdb" // database name  
    , "localhost"
    , 3306
    , nullptr
    ,"utf8mb4"
)); 

transaction t(db->begin());         
BasinInfo binfo;
binfo.setNation("中国"); //chinese character insert into column Nation
db->persist(binfo);
t.commit();

这个问题已经花了我两天时间,让我很绝望,请帮我解决这个问题,真的很感激!

附录信息:如果我在mysql workbench中查询字符集,我得到的消息与上面不同,我不知道为什么,这很奇怪。

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

Variable_name,Value
character_set_client,utf8
character_set_connection,utf8
character_set_database,utf8mb4
character_set_filesystem,binary
character_set_results,utf8
character_set_server,utf8mb4
character_set_system,utf8
collation_connection,utf8_general_ci
collation_database,utf8mb4_general_ci
collation_server,utf8mb4_general_ci

附录信息 2: 我用 utf8 字符集重新创建了数据库,一切都保持不变,多么绝望!

我仔细检查了utf8的visual studio设置,发现默认设置是带签名的UTF-8,将其更改为不带签名的utf-8,问题就解决了!