环境:
Python+Flask+MySQL
插入数据报错:
ERROR (1366, "Incorrect string value: '\xF0\x9F\x8C\xB8\xE5\x95...' for column 'wenan' at row 1")
(插入的中文数据中有表情图案)
解决方法:
将数据库字符集修改为:utf8mb4
修改数据库、表、字段的字符集命令
ALTER SCHEMA `fls`
DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci ;
ALTER TABLE `fls`.`tbl_share`
CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_general_ci ;
ALTER TABLE `fls`.`tbl_share`
CHANGE COLUMN `wenan` `wenan` VARCHAR(100) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci' NULL DEFAULT NULL ;
修改数据库连接Python代码
charset='utf8mb4'
# 连接MySQL数据库
def connectMysqlDatabase():
conn = pymysql.connect(
host='127.0.0.1',
user='',
passwd='',
port=3306,
db='fls',
charset='utf8mb4')
return conn
网友评论