如何使用Rails运行简单的MYSQL查询 [英] How to run simple MYSQL query using rails
本文介绍了如何使用Rails运行简单的MYSQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用rails运行一个简单的MYSQL查询
I want run a simple MYSQL query using rails
Select movie-title, movie-director from moving order by rating desc limit 5;
我不希望所有开销创建模型.我只想运行查询.
I don't want all the overhead creating models. I just want to run the query.
做到这一点的最佳方法是什么?
我什至无法连接
What is the best way to do this?
I cannot even connect
这是我的控制器中的代码
Here is my the code from my controller
ActiveRecord::Base.establish_connection ({
:adapter => "mysql2",
:host => "some-rds.amazon.com",
:username => "root",
:password => "root",
:database => "blah"})
这将产生此错误 ActiveRecord :: ConnectionNotEstablished
This will generate this error ActiveRecord::ConnectionNotEstablished
谢谢
推荐答案
movie_title = 'Planet of the Apes'
sql = "SELECT * FROM movies WHERE title = #{ActiveRecord::Base.sanitize(movie_title)}"
ActiveRecord::Base.connection.execute(sql)
这篇关于如何使用Rails运行简单的MYSQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文