0

I'm using Go sqlx package to make queries on MariaDB database and I'd like to be able to save non-ascii form submitted values into the database.

Here is the function:

func QuoteCreate(content string, author string) error {
    var err error
    fmt.Println("content, author", content, author)

        _, err = database.SQL.Exec("INSERT INTO quote (content, author) VALUES (?,?)", content, author)

        if err != nil {
        fmt.Println(err)
    }
    return standardizeError(err)
}

The quote tables has utf8_general_cli colation and InnoDB engine. However the content and author values submitted by form are saved as ??? when the values are not ascii characters (I tried Persian and simplified chinese).

I have also tried utf8_unicode_cli for the table colation, but got the same problem. Interestingly, non-ascii characters are shown correctly when printed on terminal befor being saved.

So I'm left clueless what's wrong here and how can I fix it?

Karlom
  • 13,323
  • 27
  • 72
  • 116
  • 1
    What encoding is your page, form and server side expecting. Often these default to ISO8859-1, unless you specify explicitly. – Andre M Oct 16 '16 at 23:58
  • I have `` in the template. – Karlom Oct 17 '16 at 00:02
  • what tool are you using to see the content of your db ? are you getting some error in your Go code ? – Yandry Pozo Oct 17 '16 at 00:49
  • @YandryPozo I use phpmyadmin to see the database. I also see the saved characters as rendered in go templates. Both cases show the same `???`. I see no error in terminal. – Karlom Oct 17 '16 at 00:58
  • As an FYI, the charset meta is only for controlling the rendition of the page. When sending form values there a number of other steps where the character encoding can fall to some default value, before getting to the database. – Andre M Nov 05 '16 at 19:01

1 Answers1

0

I don't know why but these solved the issue:

ALTER DATABASE mydb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE quote CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Karlom
  • 13,323
  • 27
  • 72
  • 116
  • 1
    Read about question marks here: http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 17 '16 at 20:49