针对多个数据库执行原始SQL

时间:2017-03-22 14:13:05

标签: ruby-on-rails sql-server sqlite activerecord

这是后续行动 executing raw sql。我正在开发一个项目,我需要从多个数据库中传输信息。我该怎么做

sql = "Select * from ... your sql query here"
records_array = ActiveRecord::Base.connection.execute(sql)

但是支持选择连接??

1 个答案:

答案 0 :(得分:2)

您可以使用onItemClick();切换数据库连接。代码应该是这样的:

ActiveRecord::Base.establish_connection

您可以在Rails documentation中找到有关#database.yml development: adapter: postgresql host: 127.0.0.1 username: postgres password: postgres database: development_db development_another_db: adapter: postgresql host: 127.0.0.1 username: postgres password: postgres database: another_db ActiveRecord::Base.establish_connection :development_another_db sql = "Select * from ... your sql query here" records_array = ActiveRecord::Base.connection.execute(sql) ActiveRecord::Base.establish_connection :development sql = "Another select" records_array = ActiveRecord::Base.connection.execute(sql) 的详细信息。