如何在没有模型的情况下从Rails应用程序查询MySQL数据库?
如何从Rails应用程序执行SQL查询到MySQL数据库?
我的应用程序使用Postgres作为主数据库,但我需要从辅助MySQL数据库中读取一些信息。 我无法创建模型,因为MySQL数据库有超过100个表,以不兼容的方式为每个表命名。 可以在没有ActiveRecord或其他方式的情况下完成吗?
你可以直接使用mysql2 gem。 请阅读此处的文档: https : //github.com/brianmario/mysql2
要么:
您可以像这样创建一个像MysqlConnection这样的新类:
class MysqlConnection < ActiveRecord::Base self.establish_connection(:adapter => 'mysql', :database => 'some-database-name') # Set all the other required params like host, user-name, etc end
从现在开始,你可以做到,
MysqlConnection.connection.select_all("SELECT * FROM table_name")
请点击链接了解如何在database.yml中存储配置详细信息: http : //weare.buildingsky.net/2006/12/06/multiple-concurrent-database-connections-with-activerecord