Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot insert emoji text #146

Open
pelli-chen opened this issue Mar 18, 2019 · 3 comments
Open

cannot insert emoji text #146

pelli-chen opened this issue Mar 18, 2019 · 3 comments

Comments

@pelli-chen
Copy link

No description provided.

@pelli-chen
Copy link
Author

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".

@pelli-chen pelli-chen changed the title emoji cannot insert emoji text Mar 18, 2019
@ziutek
Copy link
Owner

ziutek commented Mar 18, 2019

Did you send SET NAMES utf8 command before?

@pelli-chen
Copy link
Author

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&......" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants