Friday, July 18, 2014

Erlang unixODBC to Oracle


Tested in CentOS 6.5, OTP-R15B03, Oracle 11.2
NOTE examples below assume that:
Oracle server/client are both installed on localhost as instructions here:
http://zmstone.blogspot.se/2014/07/oracle-11g-serverclient-in-cetos-65.html
Test database, username and passwords are created as instructions here:
http://zmstone.blogspot.se/2014/07/create-test-database-and-roles-in-fresh.html
  1. Install some possibly missing clients/libraries [1]
    oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
    oracle-instantclient11.2-odbc-11.2.0.4.0-1.x86_64.rpm
  2. Install unixODBC
    1. download unixODBC-2.3.2.tar.gz from http://www.unixodbc.org/
    2. build unixODBC:
      ./configure --prefix=/usr --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE
      # enable iconv library to make it work with unicode.
      make
      # if everything goes fine
      sudo make install
  3. Edit unixODBC config files
    1. /etc/odbc.ini
      [TEST]
      Driver          = Oracle-11g
      ServerName      = //localhost:1521
      Database        = test
      
    2. append to /etc/odbcinst.ini
      [Oracle-11g]
      Description     = Oracle ODBC driver for Oracle 11g
      Driver          = /usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1
      Driver64        = /usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1
      Setup           = 
      Setup64         = 
      UsageCount      = 
      CPTimeout       = 
      CPReuse         = 
      FileUsage       = 1 
      Driver Logging  = 7
      
  4. Test unixODBC oracle using isql
    BASH> isql -v test tester1 SECRET
    [01000][unixODBC][Driver Manager]Can't open lib '/usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1' : file not found
    [ISQL]ERROR: Could not SQLConnect
    This does not necessarily mean libsqora.so.11.1 is missing, as it's been investigated in [2], it's probably libodbcinst.so.1 missing.
    To fix it, LD_LIBRARY_PATH should be updated:
    export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib:/usr/lib64
    # and make a soft link like below, since it's unixODBC-2.3.2 we installed,
    # but libsqora.so is looking for libodbcinst.so.1
    sudo ln -s /usr/lib64/libodbcinst.so.2.0.0 /usr/lib64/libodbcinst.so.1
    
    Now it should work (tab0 in the example below is created here: http://zmstone.blogspot.se/2014/07/create-test-database-and-roles-in-fresh.html)
    BASH> isql -v test tester1 SECRET
    +---------------------------------------+
    | Connected!                            |
    |                                       |
    | sql-statement                         |
    | help [tablename]                      |
    | quit                                  |
    |                                       |
    +---------------------------------------+
    SQL> select * from tab0;
    +-------------+---------------------------------+
    | K           | V                               |
    +-------------+---------------------------------+
    | 1           | test value                      |
    +-------------+---------------------------------+
    SQLRowCount returns -1
    1 rows fetched
    
  5. Try connect to oracle from Erlang shell [3]
    NOTE: If Erlang OTP was built from source code, and lib/erlang/lib/odbc-* can not be found in OTP install directory, you'll have to install libodbc first, then redo configure and make install.
    BASH> erl
    Erlang R15B03 ......
    
    Eshell V5.9.3.1  (abort with ^G)
    1> odbc:start().
    ok
    2> {ok, Pid} = odbc:connect("DSN=test;UID=tester1;PWD=SECRET", [{scrollable_cursors, off}]).
    {ok,<0 data-blogger-escaped-.39.0="">}
    3> odbc:sql_query(Pid, "select * from tab0").    
    {selected,["K","V"],[{1.0,"test value"}]}
References
[1] https://community.oracle.com/thread/1115824
[2] http://en.it-usenet.org/thread/876/22511/
[3] http://www.databaseskill.com/331542/

No comments:

Post a Comment