0

我不会说英语,请容忍我的表达。当我通过 odb 将一些中文字符插入 mysql 5.7 时,我收到错误消息“Incorrect string value: '\xD6\xD0\xB9\xFA' for column 'NATION' at row 1”,我知道这是错误的字符设置设置,我尝试了我的努力来解决这个问题,但都是徒劳的。

我的项目中使用了 odb 2.4、mysql 5.7、mysql connector 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工作台中查询字符集,我得到的消息与上面不同,我不知道为什么,太奇怪了。

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 字符集重新创建了数据库,一切都保持不变,多么绝望!

4

1 回答 1

0

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

于 2019-08-02T01:08:43.767 回答