Open pelli-chen opened 5 years ago
Did you send SET NAMES utf8
command before?
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&......" ?
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".