如何使用RMYSQL将字符串变量/参数传递给mysql

时间:2015-03-27 13:51:38

标签: mysql rmysql

我有一个名为time的字符串变量,它保存时间值,如:

time = "2015.03.04"; 

我想将此变量传递给mysql以检索信息

 rs <- dbSendQuery(mydb, "SELECT * FROM TIMETABLE WHERE Time LIKE",time," 13:30%");

现在打印出以下错误:

Error in mysqlExecStatement(conn, statement, ...) : 
  unused arguments ("2015.03.04", " 13:30%")

现在这是我到目前为止所得到的

sqlStatement <- paste("SELECT * FROM TIMETABLE WHERE Time LIKE '",time," 13:30%'")
sqlStatement

结果是:

> sqlStatement
[1] "SELECT * FROM TIMETABLE WHERE Time LIKE ' 2015.03.04  13:30%'"

我无法摆脱'和2015

之间的空白区域

2 个答案:

答案 0 :(得分:1)

猜我想自己回答这个问题,感谢所有的帮助!

我的解决方案似乎有点愚蠢,但解决了问题:)

time ="'2015.03.05";
sqlStatement <- paste("SELECT * FROM TIMETABLE WHERE Time LIKE ",time,"13:30%'")

答案 1 :(得分:0)

您的问题是由paste()引起的。您只需将“sep”参数设置为空字符串,因为默认值为空格。要使用您的示例:

sqlStatement <- paste("SELECT * FROM TIMETABLE WHERE Time LIKE '",time," 13:30%'", sep="")

paste documentation is here

快乐的查询!