MySQL 1366错误-使用utf8mb4编码

时间:2018-07-02 16:45:42

标签: mysql mysql-python

def connect():
    conn = mysql.connector.connect(host='localhost', database='otrs',     user='root', password='password', autocommit=True)
    if conn.is_connected():
        print('connected')
    sqlstr = "SELECT ticket.id, article.id, ticket_history.create_time, article.a_body FROM ticket, ticket_history, article WHERE ticket_history.ticket_id=ticket.id AND ticket_history.article_id=article.id AND (ticket.ticket_state_id=2 OR ticket.ticket_state_id=3) AND ticket_history.name ='%%Close' ;"

    cursor.execute(sqlstr)
    for row in cursor.fetchall():
        print row
        val= row[3].replace('\'','')
        print val
        sqlstr1 = "INSERT INTO temp VALUES (%s, %s, '%s','%s')" %(row[0], row[1], row[2], val)
        cursor.execute(sqlstr1)
        print 'done'

我编写了一个python查询,以便从mysql表中的表中插入选择数据并将其写入到临时表中。当我执行查询时,在插入几行数据后,它会引发类似

的异常
DatabaseError: 1366 (HY000): Incorrect string value: '\xE2\x80\x8BWil...'
DatabaseError: 1366 (HY000): Incorrect string value: '\xE2\x80\x8BVid...'
DatabaseError: 1366 (HY000): Incorrect string value: '\xE2\x80\x8BSol...'

引发问题的条目是

(2932, 10503, datetime.datetime(2016, 10, 19, 17, 2, 7), u'Hi Arshadh,\n\nThis has been configured on PR FWSM device only\n\nBR,\n\u200bViduna\n\xa0')
(3136, 13353, datetime.datetime(2016, 11, 25, 12, 40, 35), u'This has been postponed as we need support from forinet TAC team to resolve\nthis.\nWaiting for their feedback.\n\u200bWill raise new ticket when we get update from them\n')
(3661, 18395, datetime.datetime(2017, 1, 27, 15, 34, 45), u'This request has been performed on 1/26/2017,\n\u200bSince the testing is getting delayed- closing the cr\xa0\nwe can reopen this again if there is any problem.\n')

但是下面的数据集不会引发错误,

(3672, 18393, datetime.datetime(2017, 1, 27, 15, 28, 9), u'This request has been performed on 1/26/2017,\nSince the testing is getting delayed- closing the cr\xa0\nwe can reopen this again if there is any problem.\n')

因此如果存在\ n \ u200b而不是\ n,则会引起问题。 我到处搜索,但找不到解决方案。我认为错误是由于ASCII特殊字符引起的。但是我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

访问https://mathiasbynens.be/notes/mysql-utf8mb4 这是因为MySQL的utf8字符集仅部分实现了正确的UTF-8编码。因此,将数据库转换为utf8mb4可以解决此问题。

对于每个数据库:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

对于每个表:

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

执行这些查询,解决了该问题。