pixey pixey - 2 years ago 69
SQL Question

MySQL query in R to compare with certain value

args <- commandArgs(trailingOnly = TRUE)
id = as.character(args)
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '', dbname = 'manu',host = 'localhost')

sql<-sprintf("select * from net where ips1=%s;",id)
up = dbGetQuery(mysqlconnection, sql)

I am trying to retrieve records from the table net using R.
I want to retrieve the records with a specific id which is being passed as a command line argument.However i am getting an error near " ips1=%s ",saying that the SQL syntax which i used is incorrect. Any help?

Answer Source

Please, try to enclose the string value to compare in single quotes ':

sql <- sprintf("select * from net where ips1='%s';",id) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download