This question already has answers here:
Pass string variable in R script to use it in SQL statement
                                
                                    (3个答案)
                                
                        
                                3年前关闭。
            
                    
 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)


我正在尝试使用R从表网中检索记录。
我想检索具有特定id的记录,该记录将作为命令行参数传递。但是,我在“ ips1 =%s”附近遇到错误,说我使用的SQL语法不正确。有什么帮助吗?

最佳答案

请尝试将字符串值括起来以用单引号'进行比较:

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

关于mysql - R中的MySQL查询与特定值进行比较,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37960002/

10-12 20:45