如何将烧瓶中的表情符号保存到mysql?

时间:2019-05-21 14:58:16

标签: python mysql flask unicode emoji

UTF 8

我已使用表和数据库并将其更改为

  • utf8mb8_bin
  • utf8mb8_general_ci
  • utf8mb4_unicode_ci

这些解决方案似乎没有帮助

字符串以utf-8(3位)格式从python发送到MySQL

  

utf8使用3位

并且表情符号需要

  

4位(由于utf8mb4)

How to use variables in SQL statement in Python?

How to store Emoji Character in MySQL Database

我创建了一个表格

<html>
<head>
    <title>Test2</title>
</head>
<body>
    <form action="insert_emoji_test" method="POST">
        <input type="text" placeholder="enter text" name="myinput">
        <input type="submit" value="submit">
    </form>
</body>
</html>

所以我给表情符号字段myinput vvg 现在我想将此表情符号保存到MySQL数据库中

from flask import Flask, render_template, request
from flask_mysqldb import MySQL

app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'test2'
# Tried but didn't help
# app.config['MYSQL_CHARSET'] = 'utf8mb4'
mysql = MySQL(app)

@app.route('/')
def test2():
    return render_template('test2.html')

@app.route('/insert_emoji_test', methods=['POST'])
def insert_emoji_test():
    myinput = request.form['myinput']
    print('MyInput : \t' , myinput)
    cur = mysql.connection.cursor()
    sql_query = 'INSERT INTO test2 VALUES (' + myinput + ')'
    cur.execute(sql_query)
    cur.close()
    return render_template('test2_result.html', result = myinput)

if __name__ == '__main__':
    app.run(debug = True)

存储值时出现此错误

MySQL 1300,“无效的utf8字符串

enter image description here

  

MySQLdb._exceptions.OperationalError

     

MySQLdb._exceptions.OperationalError:(1300,“无效的utf8字符串:'vv \ xF0 \ x9F \ x98 \ x83g'”)

我将mysql表和数据库排序规则更改为 utf8mb4_unicode_ciutf8mb4_general_ci

我的主要目的是

要从表单中检索表情符号并将其作为表情瓶应用程序中的表情符号保存到mysql

0 个答案:

没有答案
相关问题