【问题标题】:MySQL problem with showing Persian charatersMySQL 显示波斯字符的问题
【发布时间】:2020-04-17 22:43:39
【问题描述】:

我有一个 Flask 应用程序,它的数据库是 MySQL。我正在尝试对包含波斯数据字段的表之一执行 SELECT 查询。查询输出是问号 (?) 而不是波斯字符。 这是我的数据库字符集:

+--------------------------+---------+
| Variable_name            | Value   |
+--------------------------+---------+
| character_set_client     | utf8    |
| character_set_connection | utf8    |
| character_set_database   | utf8mb4 |
| character_set_filesystem | binary  |
| character_set_results    | utf8mb4 |
| character_set_server     | utf8    |
| character_set_system     | utf8    |
+--------------------------+---------+

这是我在服务器上执行 SELECT 查询时的波斯字符:

mysql> select dComment from dailyLeave;

+---------------------------------------------------+
| dComment                                          |
+---------------------------------------------------+
| مرخصی یک روزه به دلیل کسالت                       |
| مرخصی اجباری!                                     |
+---------------------------------------------------+

这是我在 Python 代码中执行同一查询时的输出:

cur, conn = connection()
cur.execute('SELECT dComment FROM dailyLeave')

(('????? ?? ???? ?? ???? ?????',), ('????? ??? ???? ????',))

【问题讨论】:

    标签: python mysql utf-8 farsi


    【解决方案1】:

    我自己找到了解决方案,这很容易:) 在执行查询之前,在代码中写下这些:

    db.set_character_set('utf8')
    dbc.execute('SET NAMES utf8;')
    dbc.execute('SET CHARACTER SET utf8;')
    dbc.execute('SET character_set_connection=utf8;')
    

    db 是 MySQLdb.connect() 的结果,dbc 是 db.cursor() 的结果。

    感谢The person who posted this

    【讨论】:

    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多