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特殊字符造成的。但我不知道如何解决这个问题。