1

I have a structure. I encode it in a json format.

Next, I try to write this data to MySql. Table has utf8_unicode_ci.

data_json, err := json.Marshal(data)

db, err := sql.Open("mysql", ...)

stmt, err := db.Prepare("INSERT INTO my_table (data) VALUES (?)")

res, err = stmt.Exec(data_json)

But I have an error:

Error 1366: Incorrect string value: '\xF0\x9F\x8E\x81 \xD0...' for column 'data' at row 1

How I can remove or replace this characters in the string using Golang?

  • 1
    The incorrect value is the wrapped present emoji. See https://stackoverflow.com/questions/7814293/how-to-insert-utf-8-mb4-characteremoji-in-ios5-in-mysql for a possible solution. – Charlie Tumahai May 24 '17 at 15:28
  • 2
    Is the type of the `data` column `utf8mb4`? – syntagma May 24 '17 at 18:38
  • 1
    That Emoji () requires that the column be declared `CHARACTER SET utf8mb4`. – Rick James May 24 '17 at 20:46
  • I'm getting this even with the column in question having an encoding of `utf8mb4`. ‍♂️ – Daniel Feb 23 '18 at 21:25
  • The solution mentioned in the earlier comment (here: https://stackoverflow.com/a/8109948/297468 ) was what I needed. The connection was not set to use `utf8mb4_general_ci`. – Daniel Feb 23 '18 at 21:39

0 Answers0