- 论坛徽章:
- 0
|
各位同仁,问题一直困扰了好久,希望在这里能有个答案:
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.00 sec)
函数如下:- DELIMITER $
- DROP FUNCTION IF EXISTS `freeagent`.`chs_test`$
- CREATE FUNCTION `chs_test`(seconds INT) RETURNS VARCHAR(255)
- BEGIN
- DECLARE str VARCHAR(255) CHARACTER SET 'utf8';
- SET str = '这是汉字';
- RETURN str;
- END$
- DELIMITER ;
复制代码 将函数执行进去:
[root/tmp]# mysql -uroot -proot testdb < chtest.sql
1. 可以看到,client端的字符集是latin1,select一切正常
mysql> select chs_test(1)
-> ;
+-------------+
| chs_test(1) |
+-------------+
| 这是汉字 |
+-------------+
1 row in set (0.00 sec)
2. 如果用utf8字符集,结果是乱码:
mysql> set names 'utf8';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| 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 |
+--------------------------+--------+
7 rows in set (0.00 sec)
mysql> select chs_test(1);
+------------------+
| chs_test(1) |
+------------------+
| ÕâÊǺº×Ö |
+------------------+
1 row in set (0.00 sec)
3. 用gbk同样是乱码
mysql> set names 'gbk';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.00 sec)
mysql> select chs_test(1);
+-------------+
| chs_test(1) |
+-------------+
| ??????¡Á? |
+-------------+
1 row in set (0.00 sec)
字符集到底应该怎么设置呢?貌似函数里面的中文不太好设置。我的经验是,在导入数据的时候客户端字符集用gbk,这样select的时候都能OK。
上面的函数我在往数据库执行的时候,尝试了utf8跟gbk都不work。
谢谢 |
|