Incorrect String error when adding emoji to a database through a form

后端 未结 3 1808
终归单人心
终归单人心 2021-01-20 02:09

I can successfully add emoji (i.e. utf8mb4 data) to tables using mysql using the terminal.

When my Python Flask website tries to send emoji to the same database tabl

相关标签:
3条回答
  • 2021-01-20 02:45

    I encountered almost the same problem recently, where accented characters were causing PHP json_encode() to complain about "malformed UTF8 characters." Much to-ing and fro-ing in the documentation eventually led me to a paragraph at the bottom of the page 10.5 Character Set Configuration which states:

    … when character_set_system differs from character_set_server or character_set_client, and you input characters manually (as database object identifiers, column values, or both), these may be displayed incorrectly in output from the client or the output itself may be formatted incorrectly.

    In fact, character_set_system defaults to utf8 while character_set_server defaults to latin1 — I dare not speculate why.

    My solution was to explicitly set character_set_server = utf8 (it defaults to latin1) and collation_server = utf8_general_ci (it defaults to latin1_swedish_ci) in the [mysqld] section of my my.cnf configuration file and then restart the service. The fact that these settings differed from the corresponding *_system settings apparently was the cause of my problem.

    Some experimentation has confirmed that character_set_system must be utf8 or the server won't start. The documentation says that character_set_database can be set differently from character_set_server, but I'm not currently equipped to test the effects of this.

    0 讨论(0)
  • 2021-01-20 02:55

    If you want to store unicode character into the database you need to Define column character set to utf8mb4.

    Also change character set of table if it is required.

    it will help you store data in unicode formate

    Thanks

    0 讨论(0)
  • 2021-01-20 03:01

    Python Flask defaults to communicating with MySQL in MySQL's utf-8, i.e. it can't handle the full utf8mb4 range (which includes emojis). Flask will override the database charset settings, including the character-set-server setting in my.cf. Adding the following setting to the Flask app fixes the problem by forcing it to communicate with MySQL in utf8mb4:

    app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4'

    0 讨论(0)
提交回复
热议问题