MySQL Connector/Python如何写入emoji表情等长字节特殊符号?

1.MySQL中定义对应的表/字段的字符编码为utf8mb4

MySQL引入utf8mb4将最大存储3字节字符长度的utf8扩展到4字节,以存储包括emoji表情在内的4字节长字符文本。所以使用MySQL Connector/Python将长字节特殊符号写入MySQL前,需要先保证正确的表定义。例如,以下`chat_message_record`.`message`字段。

CREATE TABLE `test`.`chat_message_record` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'PK',
	`sender` VARCHAR(25) NOT NULL COMMENT 'message sender',
	`message` VARCHAR(255) NOT NULL COMMENT 'message detail' COLLATE 'utf8mb4_general_ci',
	PRIMARY KEY (`id`)
)
COMMENT='chat message record'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

2.Python中使用MySQL Connector/Python的C扩展API建立连接并初始化

# !/usr/bin/python
# -*- coding: utf-8 -*-


import _mysql_connector


CON = {
	"host" : "127.0.0.1",
	"port" : 3306,
	"user" : "root",
	"password" : "1024",
	"database" : "test"
}

SET = {
	"charset": "utf8",
	"use_unicode": True,
        "autocommit" : False,
}


def mysqlcon():
	"""
	Get MySQL connection in utf8mb4.
	:return: MySQL connection object.
	"""
	con = _mysql_connector.MySQL()
	con.connect(**CON)
	con.set_character_set(SET["charset"])
	con.use_unicode(SET["use_unicode"])
        con.autocommit(SET["autocommit"])
	con.query("SET NAMES utf8mb4;")
	con.query("SET CHARACTER SET utf8mb4;")
	con.query("SET character_set_connection=utf8mb4;")
	con.commit()
	return con

3.Python中使用MySQL Connector/Python的C扩展函数escape_string()将写入文本转换为SQL字符串

con = mysqlcon()
MSG = {
	"sender" : "SYSTEM",
	"message" : """
                |\_/|
                | ・x・ |
       \_____/    |
         |         |
        \       ノ 
     ((( (/ ̄ ̄ ̄ ̄(/ヽ)
	"""
}
MSG["message"] = con.escape_string(MSG["message"]).decode("utf-8")
try:
    con.query("INSERT INTO `test`.`chat_message_record` (`sender`, `message`) VALUES ('{sender}', '{message}');".format(**MSG))
except _mysql_connector.MySQLInterfaceError as E:
    con.rollback()
    import sys
    sys.exit(E)
else:
    con.commit()
finally:
    con.close()

 

你可能感兴趣的:(#,MySQL,/,MariaDB,#,Python)