简体   繁体   English

SQLite:使用变量进行字符串插值

[英]SQLite: String interpolation using variables

I am making an ajax request in my Rails 4 app and I cannot get my search query to function properly. 我在Rails 4应用程序中提出了ajax请求,但我的搜索查询无法正常运行。 I have 1 search that works where I query: 我有1个在我查询的地方有效的搜索:

list = list.where(TARGET_NAME like :search or BAR like :search, search: "%#{params[:search]["value"]}%")

which outputs the following: 输出以下内容:

SELECT COUNT(*) FROM "list" WHERE (TARGET_NAME like '%x%' or BAR like '%x%')

I have another search where the thing being searched is also a variable and I tried to copy the query above with the following: 我有另一个搜索,正在搜索的东西也是一个变量,我尝试使用以下命令复制上面的查询:

list = list.where(:column like :search, column: $other_list[col[0]], search: "%#{col[1]["search"]["value"]}%"

which outputs the following: 输出以下内容:

SELECT COUNT(*) FROM "list" WHERE ('TARGET_NAME' like '%x%')

Due to the fact that TARGET_NAME is in quotes in the query, I get 0 results returned. 由于查询中的引号中包含TARGET_NAME的事实,因此我得到了0条返回的结果。 How do I inject a variable there without the quotes? 如何在没有引号的情况下注入变量?

我敢肯定有一种方法可以通过变量来完成此操作,但是我只是用$other_list[col[0]]替换了变量:col ,从而发出了请求:

list = list.where("#{$other_list[col[0].to_i]} like :search", search: "%#{col[1]["search"]["value"]}%")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM