获取“1366(HY000):不正确的字符串值:”指定编码后

时间:2018-04-08 01:30:29

标签: python mysql encoding

抱歉,我之前看过这个问题,但似乎无法解决问题。我正在尝试正确编码我的SQL列,因此它将接受表情符号:

def create_table():
    op = '''CREATE TABLE conversation (
    text TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL,
    intent TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
    entities TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
    reply TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL
    )'''
    cursor.execute(op)

def insert_message(text, intent, entities, reply):
    try:
        #avoid using placeholders to prevent SQL injection, will pass text, intent and entities to transaction_builder() instead
        sql = """INSERT INTO conversation  (text, intent, entities, reply) VALUES (%s, %s, %s, %s)"""
        transaction_builder(sql, text, intent, entities, reply)
    except Exception as e:
        print ('error is', str(e))

with open('C:/Users/ELITEBOOK/documents/github/chatbot/chatbot/bot/robot_text.txt','r') as table:
        for robo_line in table.readlines():
            message_reply = robo_line
            message_intent = ''
            message_entities = ''
            insert_message(message_text, message_intent, message_entities, message_reply)

缺少一些代码,但这是相关的。它在我看来应该设置编码,但它似乎不是。对不起,如果这是重复的

编辑:

追溯

1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x81\xF0\x9F...' for column 'reply' at row 1

0 个答案:

没有答案