Visa ett inlägg
Oläst 2008-12-12, 12:16 #20
martines avatar
martine martine är inte uppkopplad
Mycket flitig postare
 
Reg.datum: Mar 2005
Inlägg: 767
martine martine är inte uppkopplad
Mycket flitig postare
martines avatar
 
Reg.datum: Mar 2005
Inlägg: 767
Citat:
Originally posted by Magnus_A@Dec 12 2008, 12:45
Konvertera latin1 till utf8 går bra med martines metod, men jag har inte fått det att fungera på kolumner där jag fått in utf8 i latin1-kolumner.
Ingen skulle vara gladare än jag om jag får binary-metoden att fungera, så hjälp mig gärna att hitta rätt.
Jag har dock använt för många timmar på att försöka konvertera fält och databaser utan att nå önskade resultat, så jag använder numer andra genvägar.
Genvägar? Omvägar menar du? (Förresten gjorde du en utmärkt analys av vad problemet var så jag hoppas att du inte bara tycker jag gnäller.)

Jag kan bara bekräfta att jag ett flertal gånger har konverterat (via BINARY) utf8-kolumner som varit deklarerade som latin1 så att de blir korrekta (precis som du säger uppstår problemet på genom att MySQL innan version 5 (egentligen 4.1 för att vara exakt) inte hade något stöd för unicode vilket gjorde att man var tvungen att spara utf-8 i latin1-deklarerade kolumner).

Dokumentationen för MySQL förklarar som sagt hur man gör (länkad i inlägget ovan):
Citat:
If you want to change the table default character set and all character columns (CHAR, VARCHAR, TEXT) to a new character set, use a statement like this:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
For a column that has a data type of VARCHAR or one of the TEXT types, CONVERT TO CHARACTER SET will change the data type as necessary to ensure that the new column is long enough to store as many characters as the original column. For example, a TEXT column has two length bytes, which store the byte-length of values in the column, up to a maximum of 65,535. For a latin1 TEXT column, each character requires a single byte, so the column can store up to 65,535 characters. If the column is converted to utf8, each character might require up to 3 bytes, for a maximum possible length of 3 × 65,535 = 196,605 bytes. That length will not fit in a TEXT column's length bytes, so MySQL will convert the data type to MEDIUMTEXT, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a VARCHAR column might be converted to MEDIUMTEXT.

To avoid data type changes of the type just described, do not use CONVERT TO CHARACTER SET. Instead, use MODIFY to change individual columns. For example:

ALTER TABLE t MODIFY latin1_text_col TEXT CHARACTER SET utf8;
ALTER TABLE t MODIFY latin1_varchar_col VARCHAR(M) CHARACTER SET utf8;
If you specify CONVERT TO CHARACTER SET binary, the CHAR, VARCHAR, and TEXT columns are converted to their corresponding binary string types (BINARY, VARBINARY, BLOB). This means that the columns no longer will have a character set and a subsequent CONVERT TO operation will not apply to them.

If charset_name is DEFAULT, the database character set is used.

Warning
The CONVERT TO operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column:

ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when you convert to or from BLOB columns.

To change only the default character set for a table, use this statement:

ALTER TABLE tbl_name DEFAULT CHARACTER SET charset_name;
The word DEFAULT is optional. The default character set is the character set that is used if you do not specify the character set for columns that you add to a table later (for example, with ALTER TABLE ... ADD column).
Tyvärr har jag ingen tabell som jag kan testa på just nu och orkar inte heller göra en test. Men det ska inte vara särskilt svårt (det brukar vara steget med BINARY som de flesta missar). Jag hoppas glädjen infinner sig…

Edit: Relevant mening i dokumentationen nu röd.
martine är inte uppkopplad   Svara med citatSvara med citat