Today, I spent a long time to find a bug. I read code, but finally I found the cause is that the character set of table is not correct.
Record this to avoid making the same mistake. Character set should always be set while a table is created. For MySQL, the syntax is:
1
2
3
4
5
6
7
CREATE TABLE tbl_name (column_list)
[[DEFAULT] CHARACTER SET charset_name]
[COLLATE collation_name]]
ALTER TABLE tbl_name
[[DEFAULT] CHARACTER SET charset_name]
[COLLATE collation_name]
Example:
CREATE TABLE t1 ( ... )
CHARACTER SET utf8;
Sometimes, you need to see what character set a database/table/column is, here is the solution I found at stackoverflow:
For Schemas:
SELECT default_character_set_name FROM information_schema.SCHEMATA S
WHERE schema_name = "schemaname";
For Tables:
SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "schemaname"
AND T.table_name = "tablename";
For Columns:
1
2
3
4
SELECT character_set_name FROM information_schema.`COLUMNS` C
WHERE table_schema = "schemaname"
AND table_name = "tablename"
AND column_name = "columnname";