RODBC not recognizing my odbc settings
Asked Answered
C

4

17

I'm running R 2.15.2 on a Red Hat Linux 6 server. My goal is to connect to a MS SQL Server Database on another machine via RODBC. I did my research and downloaded and installed the linux version of the MS SQL ODBC driver from the microsoft support website. I to had build unixODBC version 2.3.0 from source, because it is required by the windows driver and is not in the RHL repos yet (the repo version is 2.2.14).

Anyhow, after a bit of work, I finally got the driver installed and configured properly, and I can connect successfully to the SQL Server database via an isql command:

$ isql -v test testuser testpass
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

so I know I have my odbc.ini and odbcinst.ini files properly set up.

However, when I try to access the ODBC connection from within R, the following happens:

> test <- odbcDriverConnect('DSN=test;Database=RSQLTest01;Uid=testuser;Pwd=testpass')
Warning messages:
1: In odbcDriverConnect("DSN=test;Database=RSQLTest01;Uid=testuser;Pwd=testpass") :
   [RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data 
   source name not found, and no default driver specified
2: In odbcDriverConnect("DSN=test;Database=RSQLTest01;Uid=testuser;Pwd=testpass") :
   ODBC connection failed

Here's the result of the odbcDataSources command in R:

> odbcDataSources()
named character(0)

I have been doing some research and I think the solution (though I could be wrong) may have something to do with properly configuring the ODBC environment variables so that RODBC knows where to go to find odbc.ini. Based on my research, I found the following environment variables that may be relevant: $ODBCINI, $ODBCSYSINI, $ODBC_ROOT $ODBC_INCLUDE, and $ODBC_LIBS. I have a reasonable idea of what these should be set to, but I'm not sure how to set them permanently, and so that RODBC can recognize where to find the appropriate files.

Can anyone shed some light on this for me? I'm not sure I properly understand how environment variables work in linux, and particularly why isql has no trouble connecting, but RODBC can't even find the driver/data source.

NOTE: when I had the repo version of unixODBC installed, RODBC would install just fine using the install.packages('RODBC') command in R. However, after I installed unixODBC 2.3.0 from source, the RODBC install would fail because of a dependency issue, and I had to install RODBC from source using the guide posted here. Is it possible I failed to configure ODBC properly to begin with and that's why I'm having troubles now?

Convict answered 11/12, 2012 at 12:47 Comment(1)
Even in windows I found the odbcDriverConnect statement to be finicky, so much so that I never got it to work. Instead I use odbcConnect('odbc_dsn_entry', uid='username'). I know that ubuntu has a odbc manager that looks a lot like the one windows has but haven't done much with it to give more help especially in red hat. Also, I had more success with setting up the DB as both a user and system dsn in the odbc manager.Molality
N
6

Wow, I thought I was the only person working on this kind of stuff. I had to solve the same problem and found the best solution was to use rjdbc. This is much easier to configure as ODBC on linux or osx was very spotty. RJDBC performance is great as it uses the native ms sql server jar to execute the query. Here is an example right out of one of our scripts. You just need to download the sqljdbc4.jar from microsoft and then install.package("RJDBC") into your environment. I realized you are trying to get ODBC to work and I did get it to work on osx but I gave up due to time for linux.

library(RJDBC);  

drv <- JDBC('com.microsoft.sqlserver.jdbc.SQLServerDriver', '/usr/local/project/dataproviders/jdbc/sqljdbc4.jar', identifier.quote="'"); 

ch <- dbConnect(drv, 'jdbc:sqlserver://the.server.address.net;databaseName=DataWarehouse', 'USERNAME', 'PASSWORD'); 

allsupp_allprod_allproc <- dbGetQuery(ch, paste("SELECT  [Date], Sum([Fail]) as Numerator, Sum([Pass] + [Fail]) as Denominator,'' as Annotation,'True'  as 'Use for CL' FROM [PSU_YieldData] Group by [Date] Order by [Date]")); 
Negate answered 23/2, 2013 at 21:48 Comment(3)
Thank you so much!! JDBC was so much simpler than ODBCGreig
It's not yet working for me, but also the line above is not working (isql -v test testuser testpass. I'm attempting to connect with FileMaker: ch <- odbcDriverConnect('driver={FileMaker ODBC};server=serverDNS;database=odbc1;uid=admin;pwd=password;'). Any tips? I've also posted a question here: #40823958Reluctivity
@Reluctivity You should try the RJDBC instead. You will just need to download and use the filemaker jdbc driver. help.filemaker.com/app/answers/detail/a_id/13603/~/…Negate
D
2

In Ubuntu Server I set like this


  • Edit ~/.bash_profile

    nano ~/.bash_profile

  • Add this lines

    export ODBCINI=/usr/local/etc/odbc.ini export ODBCSYSINI=/usr/local/etc


Then RODBC will work nice.

Danelaw answered 29/7, 2014 at 17:40 Comment(0)
S
2

It can't find the driver because you didn't specify it! I don't understand how the DSN stuff is supposed to work either, but this works for me in a similar situation:

ch <- odbcDriverConnect('driver={SQL Server};server=servername;database=databasename;uid=username;pwd=password;')
sqlQuery(ch,'sql script',stringsAsFactors = FALSE)

It needs to be exact of course. And replace {SQL Server} with whatever driver you have. It's the tricky part. The servername is the same thing that comes up in Management Studio in the dropdown, probably "test" in your case. isql must automatically use the correct driver.

i.e. for MySQL it might be:

Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;

I just search for "mysql odbc connection string" and not anything R specific.

Sartor answered 20/2, 2015 at 1:7 Comment(0)
P
1

I also had this issue after configuring unixODBC on Mac OS 10.13.3 High Sierra. I tried configuring my .Renviron to set an R-specific environment variable for ODBCINI, but RODBC still wouldn't list my data source names (DSNs). Additionally, I couldn't get it to find the driver specified in ODBCinst.ini or even get a connection string working with RODBC::ODBCDriverConnect(), despite being able to connect with isql.

I was able to get the JDBC connection to work, but I didn't want to rely on the JVM and I already had a significant database access code written for RODBCext.

What ended up working was simply:

remove.packages('RODBC')
install.packages('RODBC', type="source") 

It seems if you install RODBC from source on mac, it picks up on the odbc.ini file and odbcinst.ini file correctly.

Thanks to this post's subtle comment for unlocking the key: http://eriqande.github.io/2014/12/19/setting-up-rodbc.html

Polytonality answered 26/3, 2018 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.