gpt4 book ai didi

mysql - 尝试通过 dbExecute() 将中文插入 MySQL 时出现 "could not run statement: Incorrect string value:"错误

转载 作者:行者123 更新时间:2023-11-29 03:22:52 28 4
gpt4 key购买 nike

我正在使用 R (IDE:RStudio) 通过 DBI.dbExecute 将一些数据插入回 MySQL。SQL如下:"INSERT INTO t_tmp(AnalysisDate, Dimension, Indicator, IndicatorType, Value) VALUES('2016-01-17', '维度1', '指标1', '指标类型', 101) 重复键更新值= 101;"

其中"Dimension","Indicator","IndicatorType"为中文

运行DBI.dbExecute时,出现错误信息:“.local(conn, statement, ...) 中的错误: 无法运行语句:不正确的字符串值:“维度”列的“\xB6\xC81”

我正在尝试将 SQL 粘贴到 mySQL CLI,并且运行正常(MySQL 编码已设置为 UTF-8)。

我该如何解决这个问题?谢谢!

最佳答案

我已经解决了这个问题。问题仍然是编码。我的 MySQL 的编码是“UTF-8”,因此在构建 SQL 后,使用 iconv(...) 将其编码转换为“UTF-8”,然后将其传递给 dbSendQuery 或 dbExecute 函数。那么代码是这样的:statement = "[your sql here with/without Unicode characters like Chinese]"; statement = iconv(x = statement, to = "UTF-8"); dbSendQuery(conn,语句)。希望这有帮助

关于mysql - 尝试通过 dbExecute() 将中文插入 MySQL 时出现 "could not run statement: Incorrect string value:"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034418/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com