如何让这个 MySQL 函数不弄乱我的字符集?
How to make this MySQL function not mess up my character set?
我需要一个 MySQL 函数来去除字符串开头和结尾的所有空格(包括换行符和制表符),因此我创建了以下函数:
DROP FUNCTION IF EXISTS WSTrim;
DELIMITER $$
CREATE FUNCTION `WSTrim`(text LONGTEXT CHARSET utf8) RETURNS LONGTEXT CHARSET utf8
NO SQL
DETERMINISTIC
BEGIN
RETURN TRIM(REPLACE(REPLACE(REPLACE(text, '\t', ' '), '\r', ' '), '\n', ' '));
END$$
DELIMITER ;
尽管它在参数声明和 return 值的规范中说了,但这个函数弄乱了我的字符集,我希望它是 utf8
。如果我这样调用这个函数:
INSERT INTO mytable (name) VALUES (WSTrim(' ČĆŽŠĐ čćžšđ '));
我收到警告
1 row(s) affected, 1 warning(s): 1366 Incorrect string value: '\xC4\x8C\xC4\x86\xC5\xBD...' for column 'name' at row 1
如何更改我的函数以不弄乱我的角色?
编辑 1:
我使用的连接字符串是
Server=myserver;Port=myport;Database=mydb;Uid=myuid;Pwd=mypwd;CharSet=utf8
此外,为了完整起见,
mysql> show variables where Variable_name like 'character%' or Variable_name like 'collation%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+--------------------------+----------------------------+
请注意,即使我连接 MySQL Workbench,也会发生同样的事情,而不仅仅是我的应用程序。
编辑 2:
INSERT INTO mytable (name) VALUES (' ČĆŽŠĐ čćžšđ ');
这会正确插入值。
编辑 3:
mysql> SELECT WSTrim(' ČĆŽŠĐ čćžšđ ');
+-----------------------------------+
| WSTrim(' ČĆŽŠĐ čćžšđ ') |
+-----------------------------------+
| ??ŽŠ? ??žš? |
+-----------------------------------+
1 row in set, 1 warning (0.01 sec)
我如何发送查询并不重要。但是,我注意到如果我输入
show function status;
其中 returned 行是:
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| mydb | WSTrim | FUNCTION | mydb@localhost | 2015-11-23 04:01:06 | 2015-11-23 04:01:06 | DEFINER | | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
出于某种原因,它用 latin1
处理函数。但是我该如何改变呢?
编辑 4:
mysql> show create database mydb;
+----------+---------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ |
+----------+---------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
根据 the documentation,SHOW FUNCTION STATUS
中的 character_set_client
和 collation_connection
列显示您在创建函数期间未连接到 utf8。您可能需要检查连接设置以确保您没有在某处定义 latin1。
此外,Database Collation
列表示数据库本身定义为 latin1。
我需要一个 MySQL 函数来去除字符串开头和结尾的所有空格(包括换行符和制表符),因此我创建了以下函数:
DROP FUNCTION IF EXISTS WSTrim;
DELIMITER $$
CREATE FUNCTION `WSTrim`(text LONGTEXT CHARSET utf8) RETURNS LONGTEXT CHARSET utf8
NO SQL
DETERMINISTIC
BEGIN
RETURN TRIM(REPLACE(REPLACE(REPLACE(text, '\t', ' '), '\r', ' '), '\n', ' '));
END$$
DELIMITER ;
尽管它在参数声明和 return 值的规范中说了,但这个函数弄乱了我的字符集,我希望它是 utf8
。如果我这样调用这个函数:
INSERT INTO mytable (name) VALUES (WSTrim(' ČĆŽŠĐ čćžšđ '));
我收到警告
1 row(s) affected, 1 warning(s): 1366 Incorrect string value: '\xC4\x8C\xC4\x86\xC5\xBD...' for column 'name' at row 1
如何更改我的函数以不弄乱我的角色?
编辑 1:
我使用的连接字符串是
Server=myserver;Port=myport;Database=mydb;Uid=myuid;Pwd=mypwd;CharSet=utf8
此外,为了完整起见,
mysql> show variables where Variable_name like 'character%' or Variable_name like 'collation%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+--------------------------+----------------------------+
请注意,即使我连接 MySQL Workbench,也会发生同样的事情,而不仅仅是我的应用程序。
编辑 2:
INSERT INTO mytable (name) VALUES (' ČĆŽŠĐ čćžšđ ');
这会正确插入值。
编辑 3:
mysql> SELECT WSTrim(' ČĆŽŠĐ čćžšđ ');
+-----------------------------------+
| WSTrim(' ČĆŽŠĐ čćžšđ ') |
+-----------------------------------+
| ??ŽŠ? ??žš? |
+-----------------------------------+
1 row in set, 1 warning (0.01 sec)
我如何发送查询并不重要。但是,我注意到如果我输入
show function status;
其中 returned 行是:
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| mydb | WSTrim | FUNCTION | mydb@localhost | 2015-11-23 04:01:06 | 2015-11-23 04:01:06 | DEFINER | | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
出于某种原因,它用 latin1
处理函数。但是我该如何改变呢?
编辑 4:
mysql> show create database mydb;
+----------+---------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ |
+----------+---------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
根据 the documentation,SHOW FUNCTION STATUS
中的 character_set_client
和 collation_connection
列显示您在创建函数期间未连接到 utf8。您可能需要检查连接设置以确保您没有在某处定义 latin1。
此外,Database Collation
列表示数据库本身定义为 latin1。