我目前使用rails 2.3.8和dbi gem來訪問mssql數據庫。生產位於交鑰匙導軌VA和dev位於Linux VM 我想弄清楚以前的開發人員做了什麼訪問數據庫服務器無濟於事,所有'有用的網站給了我各種各樣的錯誤'我不能考慮改變爲tinytds,因爲它打破了並且想要使用舊的方法。 目前,我有以下代碼設置訪問和查詢MSSQL在rails中查詢Microsoft Server 2008 2.3.8
ExternalData.rb
def self.datapoint_connection(&block)
db_connection('type1',&block)
end
def self.db_connection(connection_type, &block)
begin
conn_data = YAML::load_file("#{Rails.root}/config/eg_databases.yml")[connection_type.to_s]
driver = conn_data.delete('driver')
user = conn_data.delete('username')
password = conn_data.delete('password')
conn_params = conn_data['entry']
conn_str = "DBI:#{driver}:#{conn_params}"
conn = DBI.connect(conn_str, user, password)
if block_given?
yield conn
else
return conn
end
ensure
# disconnect from server
conn.disconnect if conn && conn.connected? && block_given?
end
end
eg_database.yml
type1:
driver: ODBC
entry: dsn_con1
username: blah
password: 'blah'
查詢另一個數據庫
def excluded_testrun_users
ExternalData.datapoint_connection do |dbh|
dbh.select_all("SELECT username FROM team where team") do |row|
puts row
end
end
end
總體查詢
def user_test_run_sql(granularity,date,end_date_condition=false)
"(select * from test_run where user_id NOT IN
(#{excluded_testrun_users.map{|u| DataPoint.sanitize(u)}.join(", ")})
#{' and testrun_end IS NOT NULL' if end_date_condition}) as sub_test_run"
end
我想知道這是否正確的方法來訪問多個數據庫在同一臺服務器在mssql下。 dsn是否必須爲每個數據庫連接更改(創建),但在相同的服務器下?
當我確實嘗試運行。我收到以下錯誤
Error getting query: Unable to load driver 'ODBC' (underlying error: uninitialized constant DBI::DBD::ODBC)
.... not gonna show stupidly long called path list to every conceivable files for ruby...
/usr/local/rvm/rubies/ruby-1.8.7-p357/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/local/rvm/rubies/ruby-1.8.7-p357/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in'需要」
script/server:3
任何見解或幫助將不勝感激
「歡迎,所以如果其中一個答案如下解決您的問題,您應該接受它(單擊複選標記旁邊合適的答案)做兩這讓大家都知道你的問題已經解決了,並且它給了幫助你信用的人。請看這裏以獲得完整的解釋「 –