0

我正在尝试编写如下准备好的声明:

@fofs = FileOrFolder.connection.select_all("select * from newtestdocB.file_or_folders where name like","%#{params[:search]}%")

原来是mysql控制台上的错误查询。不接受变量 %#{params[:search]}% 的值进行查询

请更正我的查询...

控制台消息:

Parameters: {"search"=>"do", "cluster_id"=>"2", "datasetid"=>"1", "id"=>"1"}
  %do% (0.2ms)  select * from newtestdocB.file_or_folders where name like

ActiveRecord::StatementInvalid (Mysql2::Error: 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 '' at line 1: select * from newtestdocB.file_or_folders where name like):
4

1 回答 1

0

Try a placeholder ?

"select * from newtestdocB.file_or_folders where name like ?","%#{params[:search]}%"

请参阅上述语句中的问号,该问号将由逗号后面的参数填写。

如果上面的语句不起作用,这必须起作用,使用“+”连接,注意“like”旁边的空格

sql = "select * from newtestdocB.file_or_folders where name like <<space>>" + "%#{params[:search]}%"

FileOrFolder.connection.select_all(sql)
于 2013-07-11T16:28:12.053 回答