ziutek / mymysql

MySQL Client API written entirely in Go
Other
735 stars 161 forks source link

cannot insert emoji text #146

Open pelli-chen opened 5 years ago

pelli-chen commented 5 years ago

CREATE TABLE test (id bigint auto_increment primary key, name varchar(20) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

_, err = db.Exec("INSERT INTO test (name) values ( UNHEX(?) ) ", fmt.Sprintf("%X", "a😊b")) fmt.Println(err) fmt.Printf("%X\n", "a😊b") //61F09F988A62, 插入

When inserting emoji, have to use mysql UNHEX to work-around. Direct inserting results in text "3F3F3F3F".

ziutek commented 5 years ago

Did you send SET NAMES utf8 command before?

pelli-chen commented 5 years ago

thanks, problem solved:

, err = tx.Exec("SET NAMES 'utf8mb4' "); if err != nil { panic(err) } , err = tx.Exec("INSERT INTO test (name) values ( ? ) ", "a😊b")

Btw, is is convenient to put it as a connection option, like in java JDBC url "jdbc:mysql://127.0.0.1/mydb?useUnicode=true&connectionCollation=utf8mb4_general_ci&......" ?