Consider this:
> scr<-paste("INSERT INTO ques2_log (freeze_time) value(",sQuote(now()),")")
> scr
#> "INSERT INTO ques2_log (freeze_time) value( ‘2017-06-13 23:46:16’ )"
If we feed this simple SQL script into a MySQL DB as follows:
dbExecute(db,scr1)
The MySQL DB throws the following error:Error in .local(conn, statement, ...) : could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '��2017-06-13 23:44:13’ )' at line 1
I have tested the SQL script by typing by hand and it works.
It is also clear that the single quote is the unexpected character.
I looked up some online articles on character encoding and tried
enc2utf8(scr) before feeding to the DB through RMySQL commands. No effect. Same error.
I also read this and ran
ALTER DATABASE ques2_log CHARACTER SET utf8 COLLATE utf8_general_ci;
But the error remains.
sQuoterather than just plain old single'? TypicallysQuoteis used to format output to be displayed on screen, which isn't what you're doing here.paste0("'",date(),"'")?sQuote(date(), q = FALSE)would also work but it isn't the best tool for the job. Also I would not recommend usingpaste()in this context. You are better off usingDBI::dbQuoteLiteral()or glue::glue_sql() for e.g. correctNAhandling.