0

我在让 Sequel 连接到 MS SQL 数据库时遇到问题。我已经安装了以下内容:

  • UnixODBC
  • 免费TDS

我已经配置了这两个软件包,以下命令允许我毫无问题地连接到我的数据库:

  • isql
  • tsql
  • osql

但是,当我使用 Sequel.odbc 命令从 Ruby 代码中尝试它时,我收到以下错误:

ODBC::Error: IM003 (0) [iODBC][Driver Manager]无法加载指定的驱动程序。

这是我所能得到的。我曾经首先收到另一个错误,但通过重做配置部分设法解决了这个问题。我想我错过了那里的东西。

编辑

这是我的基础谈话者类的代码。它基本上像 rails 一样加载一个 YAML 文件,保存数据库设置并建立与数据库的连接。这似乎有效,尝试手动从续集中返回一个 DB 对象:

module Talkers
  require 'yaml'
  require 'sequel'

  class BaseTalker
    # This function will load the desired settings as a hash from the database.yml file
    # in the config folder. The data will be returned as a hash following the standard
    # YAML structure.
    def self.load_config(name)
      cfg = YAML::load(File.open(File.join(ENV['API_ROOT'], 'config', 'database.yml')))
      cfg.key?(name) ? cfg[name] : nil
    end

    # This function will establish a connection with the Florensia User database and return
    # the Sequel database object, so that queries can be executed against the database.
    def self.connect_to_user_db
      settings = self.load_config("florensia_user_#{ENV['RACK_ENV']}")
      Sequel.odbc settings['dsn'], :db_type => settings['adapter'], :user => settings['user'], :password => settings['password']
    end
  end
end

下面的类继承自 talker 并为 User 执行某些操作。它包含特定于游戏的数据库逻辑。当我调用这个逻辑时,我收到错误:

module Talkers
  require 'yaml'
  require 'sequel'

  class LoginDbTalker < BaseTalker
    #
    # Bans the specified User from the game. The function requires the following information
    # to be supplied in order for the ban to be properly set:
    # - id : The identifier of the account.
    # - gm_name : The name of the GM setting the ban.
    # - punish_code : The punishment code being applied on the account.
    # - days : The duration of the ban in days, starting from today.
    #
    # The function will return true if the ban has been properly set; otherwise the function
    # will return false.
    def self.ban_user(options = {})
      return false if options.empty?
      db = self.connect_to_user_db
      ds = db[:tbl_User].filter(:id => options[:id])
      ps = ds.prepare(:update, :apply_ban)
      ps.call(
          :punishcode => options[:punish_code],
          :punishstory => "Banned by #{options[:gm_name]}",
          :punishdate => Date.today,
          :punishfreedate => (options[:days].to_i == -1) ? (Date.today + (30 * 265)) : (Date.today + options[:days].to_i))
      true
    rescue Exception => e
      puts "Exception caught in ban_user: #{e.to_s}"
      puts "Provided variables: id=#{options[:id]}, gm_name=#{options[:gm_name]}, punish_code=#{options[:punish_code]}, days=#{options[:days]}"
      false
    end
    #
    # Unbans the specified User from the game. The function requires the following information
    # to be supplied in order for the ban to be properly lifted:
    # - id : The identifier of the account.
    # - gm_name : The name of the GM removing the ban.
    #
    # The function will return true if the ban has been properly lifted; otherwise the function
    # will return false.
    def self.unban_user(options = {})
      db = self.connect_to_user_db
      ds = db[:tbl_User].filter(:id => options[:id])
      ps = ds.prepare(:update, :lift_ban)
      ps.call(
          :punishcode => '0',
          :punishstory => "Ban removed by #{options[:gm_name]}",
          :punishdate => Date.today,
          :punishfreedate => Date.today
      )
      true
    rescue Exception => e
      puts "Exception caught in unban_user: #{e.to_s}"
      puts "Provided variables: id=#{options[:id]}, gm_name=#{options[:gm_name]}"
      false
    end
    #
    # Kicks the specified User from the game, regardless on the server or character he currently is on.
    # This requires a direct connection to the game servers so a specialized command can be sent that
    # instruct the server to close the connection with the offending player.
    # The function returns true if the kick succeeded; otherwise false.
    def self.kick_player(id)
      false
    end
  end
end

调用任何禁止/取消禁止函数都会导致错误消息。

编辑2

我添加了文件夹 /Library/ODBC 并将所有配置文件链接到那里以用于 iODBC。这消除了我之前遇到的错误,现在给我带来了这个错误:

ODBC::Error: 01000 (20002) [FreeTDS][SQL Server]自适应服务器连接失败

所以看来我又取得了一些进展

4

1 回答 1

0

如果您从 OS X 连接到 Microsoft SQL Server,我建议您使用 tinytds 适配器而不是 odbc 适配器。我知道有些人在非 Windows 机器上运行 Sequel/ODBC,但我只有 Sequel/经验Windows 上的 ODBC。

于 2012-04-19T15:59:12.610 回答