I am writing some html code to an SQL databse using RMySQL (but I guess my problem is rather a general R question than really related to SQL or RMySQL). So I am trying something like this:
con <- RMySQL(...) # some connection
html <- "<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>"
query <- c('INSERT INTO table (htmlfield) VALUES (\"', html, '"')
dbSendQuery(con,paste(query, collapse = ""))
Trouble is, R's paste will replace the double quotes in single quotes (i.e. '"') to the escaped sequence \", i.e.:
> paste(query, collapse = "")
[1] "INSERT INTO table (htmlfield) VALUES (\"<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>\""
If I change the single quotes in the vector query to double quotes, and the single quotes in html to doubles, the problem is then on the side of the character string html, since then the double quotes in html get replaced by the escaped sequence.
What's the easiest way to handle a substitution of the escaped characters?
I tried gsub('\\\"','"',html)
which did not work as intended and the solutions suggested in the post Ignore escape characters (backslashes) in R strings but I could not make it work.
Thanks for your attention, Philipp