How to insert latin characters into Mysql from the command line shell?

試著忘記壹切 提交于 2020-01-26 02:57:06

问题


Why can't I insert the word Español via the command line mysql program ? See session below.

mysql> select CONCAT( 'Espa', 0xF1, 'ol' );
+------------------------------+
| CONCAT( 'Espa', 0xF1, 'ol' ) |
+------------------------------+
| Español                       |
+------------------------------+
1 row in set (0.00 sec)

mysql> create table t 
    > ( 
    >     nom varchar(25) NOT NULL 
    > ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> insert into t VALUES( CONCAT( 'Espa', 0xF1, 'ol' ) );
Query OK, 1 row affected, 1 warning (0.02 sec)

mysql> select * from t;
+------+
| nom  |
+------+
| Espa |
+------+
1 row in set (0.00 sec)

mysql> select HEX(nom) from t;
+----------+
| HEX(nom) |
+----------+
| 45737061 |
+----------+

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

回答1:


0xf1 is not a valid UTF-8 sequence, so it chokes on that. Use the proper UTF-8 sequence instead:

$ python -c "print repr('\xf1'.decode('latin-1').encode('utf-8'))"
'\xc3\xb1'



回答2:


It seems to work with latin1 as the default charset.



来源:https://stackoverflow.com/questions/2286912/how-to-insert-latin-characters-into-mysql-from-the-command-line-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!