user266057
user266057

Reputation:

MySQL charset conversion

I have a database in which all text fields and tables have 'utf8' specified explicitly as default character set, however data in this database is stored as 'cp1257'. I can display symbols only when I use SET NAMES 'cp1257' or \C cp1257. Trying to display them without those instructions fails, because it tries to fetch data as 'utf8' (?). What could I do to get all data (perhaps a backup) of database as 'cp1257' and put it back as valid 'utf8' ?

Upvotes: 0

Views: 491

Answers (1)

Michal Čihař
Michal Čihař

Reputation: 10091

You need to convert them first to binary field and then set the field as cp1257. This way MySQL will know in which charset the field is stored:

ALTER TABLE CHANGE `field` `field` BLOB;
ALTER TABLE CHANGE `field` `field` TEXT CHARACTER SET 'cp1257';

If you want to change way how data are stored, you can now convert the field to any charset (MySQL will do the conversion of data in this step):

ALTER TABLE CHANGE `field` `field` TEXT CHARACTER SET 'utf8';

Upvotes: 1

Related Questions