views:

183

answers:

2

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/ |
+--------------------------+----------------------------+
A: 

It seems to work with latin1 as the default charset.

Erik Giberti
I need to store chinese characters too, so I can't switch to latin1.
Jacques René Mesrine
A: 

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'
Ignacio Vazquez-Abrams
Great. This works. insert into t VALUES( CONCAT('Espa', 0xc3, 0xb1, 'ol' ) );
Jacques René Mesrine