How to query a remote SQLAnywhere16 db using Rails

2019-09-05 22:44发布

问题:

I am trying to set up a Rails app to query a remotely hosted SQLAnywhere 16 db and to display some query information as HTML. I'm using current versions of Ruby and Rails on Mac El Capitan. The remote server is running Windows. The server is located in my building and I am on the same LAN, so if there's a simpler solution that involves accessing the box itself physically I'm open to that.

On my Windows machine I am able to connect to the server and do queries on the db using a SQL client.

I have sqlanywhere and activerecord-sqlanywhere-adapter gems installed. I also have the SQL Anywhere 16 client installed on my (Mac) machine. The remote db set up in my database.yml file as another db:

my_external_database:
  adapter: sqlanywhere
  encoding: utf-8
  username: (username)
  password: (password)
  server: (name of server)
  database: (db name)
  host: (IP of server)

I'm calling it like so:

class MyExternalModel < ActiveRecord::Base
    establish_connection(:my_external_database)
    self.table_name = '<a particular table in the db>'
    self.primary_key = 'primarykey'
end

When I go into the rails console and do MyExternalModel.first to test the connection, I get:

LoadError: Could not load SQLAnywhere DBCAPI library

After doing some googling, this seems to be a problem with configuring SQLAnywhere. However, I believe I configured it properly (in bash using bin64 config), like so:

. /Applications/SQLAnywhere16/samples/sample_config64.sh
Enter destination directory for copy of the samples [/Users/(user)/sa16_samples]: 
Copying samples...
Done

Setting up data sources... 

SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 Demo" written to file /Users/(user)/.odbc.ini
SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 CustDB" written to file /Users/(user)/.odbc.ini
Done

Setting up sample_env script... 
Done

This is as far as I'm able to get. Any recommendations are greatly appreciated!

回答1:

I use SQLAnywhere for a production server and it works fine using the very same gem you're using. I use a Mac for development and my database is running in Windows (or Linux). In order to get rid of this error, try the command (on your Mac):

# adjust SQLAnywhere to your version -> 16
source /Applications/SQLAnywhere12/System/bin64/sa_config.sh

I'm using Rails 4 and SQLAnywhere 12 (but tested with 16 and it works too).

After that, try to load console (rails c) and do some tests there.