Charset und die Verbindung
Links zum Thema:
Vor allen Überlegungen sollte zunächst erstmal ein Backup erstellt werden. Je nach Gusto und Kenntnis bieten sich da ja einige Möglichkeiten.
Dann sollte der IST-Zustand festgestellt werden:
mysql> show variables like 'ch%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
| utf8_general_ci | utf8 | 33 | Yes | Yes | 1 |
si algien nececite vea -> {http://blog.rastersoft.com/index.php/2006/12/10/python-mysql-utf-8-y-la-madre-que-los-pario/#more-15 Convertir ] las tablas a UTF-8 Es el primer paso: si creaste tus tablas en formato Latin-1, lo mejor es pasarlas a UTF-8 para evitarse problemas. Para ello usaremos
mysqldump –opt –password=miclave –user=miuser mibasededatos > archivo.sql
A continuación editaremos el código y cambiaremos la palabra latin1 por utf8, con lo que al recuperar los datos, las tablas se crearán en el nuevo formato. Ahora sólo queda volver a grabarlas con
mysql -u miuser -p miclave mibasededatos < archivo.sql
y listo, ya tenemos nuestras tablas en UTF-8.
Acceder a MySQL mediante UTF-8 Esta parte es sencilla también, pero la documentación es escasa y sólo la encontré después de bucear por muchas páginas de Internet y juntar resultados. Tenemos que usar la línea en python:
mibase=MySQLdb.connect(host=”mihost”, user=”usuario”, passwd=”clave”, db=”basedatos”, charset=”utf8″, init_command=”set names utf8″)
(atención a la negrita). Justo a continuación tenemos que añadir:
mibase.names=”utf8″
y con ésto ya tendremos garantizado el acceso a la base de datos mediante UTF-8.
Some stuff found here
Hi djangers!
I've accidentally bumped into an IntegrityError problem as I added
unique_together = (("word", "language"),)
to my Keyword class.
Debugging the problem I was surprised to discover the following
behavior in MySQL shell:
mysql> select 'm n'='man';
+--------------+
| 'm n'='man' |
+--------------+
| 1 |
+--------------+
So it seems like MySQL cannot tell the difference between those m n
and man?! That sounds weird. It might be some MySQL options I need to
set.
Can anyone point me to some docs were I can dig for the answer? Or
just had the same issue?
It should be a collation issue.
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> select 'm n'='men';
+--------------+
| 'm n'='men' |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
You might want to use the SELECT BINARY operator:
mysql> select binary 'm n'='men';
+---------------------+
| binary 'm n'='men' |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)
Or set the default collation for the table in question.
With utf8_general_ci, which transliterates non-ascii characters:
mysql> create table test (a varchar(8), b varchar(8)) character set
utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.07 sec)
mysql> insert into test values (' ', 'e');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test where a=b;
+------+------+
| a | b |
+------+------+
| | e |
+------+------+
1 row in set (0.00 sec)
With binary collation:
mysql> alter table test character set utf8 collate utf8_bin;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into test values (' ', 'e');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test where a=b;
Empty set (0.00 sec)
Or set binary collation only for the filed which will contain unicode
chars:
mysql> create table test (a varchar(8) collate utf8_bin, b varchar(8))
character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.08 sec)
mysql> insert into test values (' ', 'e');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test where a=b;
Empty set (0.00 sec)
[ http://dev.mysql.com/doc/refman/4.1/en/charset-collations.html You can find the manual pages for MySQL collations and related issues here]
1. at first i did what Georgi Stanojevski suggested:
To get mysql client in the console working with utf-8 (doesn't have
> anything to do with Django) I have this in /etc/my.cnf:
> [client] > . > . > default-character-set = utf8
> [mysqld] > . > . > character-set-server=utf8 > collation-server=utf8_unicode_ci > init_connect='set collation_connection = utf8_unicode_ci;'
2. then i restarted the mysql server with (i use ubuntu): $ sudo /etc/init.d/mysql restart
3. from mysql client mysql> alter table polls_choice convert to character set utf8; mysql> alter table polls_poll convert to character set utf8;
and finally the input of cyrillic in the admin area started to work!
the interesting fact is that in the same time running the SELECT statement from mysql client yields: mysql> select * from polls_choice; +
+
+
+
+ | id | poll_id | choice | votes | +
+
+
+
+ | 1 | 1 | ??? ????? | 0 | | 2 | 1 | ????? ????? | 0 | +
+
+
+
+
to output of question marks, which i suppose displays, that the client doesn't handle utf8.
I want to thank all the people, who tried to help me. I will try my best to be helpful as well
}}}
-- DetlevLengsfeld 2007-04-16 17:02:40
Linux/MySql/Datenkonvertierung-nach-Utf8 (last modified 2008-11-04 07:00:05)