如何使用 Sequel 运行原始 SQL 查询
我还不清楚使用 Sequel 运行原始 SQL 查询的正确方法。
目前我正在尝试这个:
DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1") do |row|
@zonename = row
end
如何以原始 SQL 方式运行查询,然后像平常一样访问结果?
if @zonename.name = "UK"
I am not clear yet on the proper way to run raw SQL queries with Sequel.
Currently I am trying this:
DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1") do |row|
@zonename = row
end
How can I can run the queries as raw SQL then access the results like normal?
if @zonename.name = "UK"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:而不是
请注意,您应该
:否则,如果您不控制
@dialcode
的内容,您就会面临 SQL 注入。Note that instead of:
you should do:
Otherwise, you open yourself to SQL injection if you don't control the contents of
@dialcode
.我有一些可能有用的建议:
你可以简单地这样做:
注意:您忽略了可能有更多结果符合条件的事实。如果您期望返回多个可能的行,那么您可能希望通过执行...
并处理所有这些行来构建结果数组。
返回集是一个哈希值。如果
@zonename
指向其中一条记录,那么您可以这样做<前><代码> @zonename[:column_name]
名为“column_name”的字段。您不能执行
@zonename.column_name
(您实际上可以使用一些元编程的辅助方法来装饰@zonename
,但我们暂时忽略它)。Sequel 是一个优秀的界面,你了解得越多你就会越喜欢它。
I have a few pointers which may be useful:
You could simply do:
NB: you are ignoring the fact that there could be more results matching the criteria. If you expect multiple possible rows to be returned then you probably want to build an array of results by doing ...
and processing all of them.
The return set is a hash. If
@zonename
points to one of the records then you can doto refer to a field called "column_name". You can't do
@zonename.column_name
(you could actually decorate@zonename
with helper methods using some meta-programming but let's ignore that for the moment).Sequel is an excellent interface, the more you learn about it the more you'll like it.