2

I've got a linux server running RStudio, and I'm trying to connect to an SQL Anywhere database.

I have the drivers installed and configured, and I can connect using iSQL. When trying through RStudio, I continually get this error:

Error: nanodbc/nanodbc.cpp:983: 00000: [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed 

However, if I launch an R script straight from /opt/bin/r/rscript, it connects.

The same thing happens when trying to connect with Python through a conda environment in my home directory. However, if I launch by typing "python test.py" into the terminal, the connection succeeds.

I'm on Ubuntu 18.04 with the SQL Anywhere 17 drivers. Any ideas would be appriciated.

Thanks.

clarkwray
  • 23
  • 2

2 Answers2

1

I just solved this issue with a very similar setup: Connecting to a SQL Anywhere database, where the connection works from R when launched from the command line, but not from RStudio, and gives the error:

SQLAllocHandle on SQL_HANDLE_HENV failed

The key was to set the environment variables in RStudio to match those in my regular shell. In my case, these were $ODBCINI and $LD_LIBRARY_PATH. I reset them as follows:

  1. In the shell, ran the following to get the values being used by console R.

    echo $ODBCINI
    echo $LD_LIBRARY_PATH
    
  2. In RStudio, ran Sys.getenv() to confirm these values were different.

  3. Reset the variables to match with

    Sys.setenv(ODBCINI = "[path from shell]/odbc.ini")
    Sys.setenv("LD_LIBRARY_PATH" = paste0(Sys.getenv("LD_LIBRARY_PATH"),":[user path from shell]"))
    

With this setup, I was able to connect from RStudio.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

I was having this same error message with my new M1 Mac when I tried to connect with SQLite from RStudio. The RStudio solutions manual (https://solutions.rstudio.com/db/best-practices/drivers/) still only lists connection strings for odbc.ini and odbcinst.ini on an Intel Macs. When I changed my SQLite Driver in odbcinst.ini (located in /opt/homebrew/etc) after the Homebrew install to /opt/homebrew/lib/libsqlite3odbc.dylib instead of /usr/local/lib/libsqlite3odbc.dylib, it finally worked.

David Lucey
  • 252
  • 3
  • 9