代码之家  ›  专栏  ›  技术社区  ›  HMD

mysql 1366错误-使用了utf8mb4编码

  •  0
  • HMD  · 技术社区  · 6 年前
    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 回复  |  直到 6 年前
        1
  •  0
  •   HMD    6 年前

    参观 https://mathiasbynens.be/notes/mysql-utf8mb4 这是因为mysqls的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;
    

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