开发者

RMySQL wrong quote escaping

I'm using R with RMySQL library to connect to Mysql datab开发者_Go百科ase.

I have seen there is a problem with their escaping function:

> dbEscapeStrings(con, "HE''LLO")
[1] "HE\\'\\'LLO"

this is wrong, It should be: "He\'\'LLO"

Do I have to use another function to escape quotes and double quotes?


I think you are mistaking the printed R representation with the actual result from dbEscapeStrings(). In R \ also needs to be escaped. So if you want a literal \, you need two of them \\. This is how the escaped string is displayed by R when printing and explains the observed behaviour:

> foo <- dbEscapeStrings(con, "HE''LLO")
> foo
[1] "HE\\'\\'LLO"

However, note that this is just the way that escaped string is represented within R at the console. If we cat() or writeLines() the escaped string to the console, instead of print()-ing

> writeLines(foo)
HE\'\'LLO

we see that it has been properly escaped. The latter is how MySQL would see it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜