我需要一个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 ;
尽管它在参数声明和返回值的规范中说了什么,但这个函数会弄乱我的字符集,我希望它是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;
返回的行之一是:
+------+--------+----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| 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)
答案 0 :(得分:3)
根据the documentation,character_set_client
中的collation_connection
和SHOW FUNCTION STATUS
列显示您在创建函数期间未与utf8建立关联。可能你需要检查连接设置,以确保你没有在某处定义latin1。
同样,Database Collation
列表示数据库本身被定义为latin1。