【发布时间】:2017-02-13 04:47:05
【问题描述】:
是什么导致了这个不正确的字符串?我已经阅读了很多问题和答案,这是我的结果。阅读答案后,我仍然遇到同样的错误。
我收到以下错误:
ERROR 1366 (HY000) at line 34373: Incorrect string value: '\xEF\xBB\xBF<?x...' for column 'change' at row 1
当我尝试在 SQL 中输入以下内容时:
行号 34373:INSERT INTO gitlog_changes VALUES ('123456', 'NhincCommonEntity.xsd', '<?xml version=\"1.0\" encoding=\"UTF-8\"?>');
我的桌子是这样的:
DROP TABLE IF EXISTS `gitlog_changes`;
CREATE TABLE `gitlog_changes` (
`hashID` varchar(40) NOT NULL,
`filename` varchar(450) DEFAULT NULL,
`change` mediumtext
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我读了很多答案说将字符集更改为 UTF8 [1][2][3][4]。所以我执行这个:
alter table yourTableName DEFAULT CHARACTER SET utf8;
我继续收到同样的错误。那我alter table yourTableName DEFAULT CHARACTER SET utf8mb4_general_ci;
仍然出现同样的错误。
我还尝试从 python 读取文件并直接提交到数据库。从这个答案[1]。我收到的是警告而不是错误。
我将以下代码插入到我的 python 脚本中:
cursor.execute("SET NAMES 'utf8'")
cursor.execute("SET CHARACTER SET utf8")
Python 脚本:
def insert_changes(modList):
db = MySQLdb.connect("localhost", "user", "password", "table")
cursor = db.cursor()
cursor.execute("SET NAMES 'utf8'")
cursor.execute("SET CHARACTER SET utf8")
for mod in modList:
hashID = mod["hashID"]
fileName = mod["fileName"]
change = mod["change"]
cursor.execute("INSERT INTO gitlog_changes VALUES (%s, %s, %s" , (hashID, fileName, change))
# # disconnect from server
db.commit()
db.close()
我在这里得到的警告是:Warning: Invalid utf8 character string: '\xEF\xBB\xBF<?x...'
cursor.execute("INSERT INTO gitlog_changes VALUES (%s, %s, %s)" , (hashID, fileName, change))
【问题讨论】: