Connecting to Microsoft SQL Server on ODBC using R in Ubuntu

2.3k views Asked by At

I was able to get it completely working on Windows 7, but I have to move it to a Linux server. I'm running Ubuntu 16.04.1 LTS 64-bit in a VM right now for testing before I replicate the process on the server.

I'm pretty sure the issue is somewhere in my FreeTDS or DSN setup.
Should I stop wasting my time and just use Python? Or will I have this same issue with ODBC and unixODBC?

Full R script:

library(RODBC)

#saving from a .csv to dataframe df
df <- read.csv("./Documents/test.csv")

#creating connection to db
conn <- odbcDriverConnect('myDSN')

#writing
sqlSave(conn, df, tablename = 'dbo.test0', append = F, rownames = F, verbose = TRUE, safer = true, fast = F)

R Console

> conn <- odbcDriverConnect('myDSN')

Returns error message:

Warning messages:
1: In odbcDriverConnect("myDSN") :
  [RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified
2: In odbcDriverConnect("myDSN") : ODBC connection failed

Here's my ODBC and FreeTDS config:

/etc/odbc.ini

[myDSN]
APP = unixodbc
Description     = master on DBNAME
Driver          = TDSdriver
Server          = SERVNAME
Database        = DBNAME
Port            = 1433
Username = UNAME
Password = PW
#Trace           = No
#TraceFile       = /var/log/freetds/freetds--odbc.log

/etc/odbcinst.ini

[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so


[myDSN0]
APP = unixodbc
Description     = user on DBNAME
Driver          = TDSdriver
Server          = SERVNAME
Database        = DBNAME
Port            = 1433
Username = UNAME
Password = PW
#Trace           = No
#TraceFile       = /var/log/freetds/freetds--odbc.log

/freetds/freetds.conf

[global]
        # TDS protocol version
;   tds version = 4.2

    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
;   dump file = /tmp/freetds.log
;   debug flags = 0xffff

    # Command and connection timeouts
;   timeout = 10
;   connect timeout = 10

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 64512

[DSN0]
        host = IPADDRESS 
        port = 1433
        tds version = 7.2
        client charset = UTF-8
1

There are 1 answers

2
blongworth On

I have RODBC -> FreeTDS -> SQL Server working on linux Mint. It's been a while since I've configured it, but here are some (untested) suggestions:

There are some good install notes at: http://www.saltycrane.com/blog/2011/09/notes-sqlalchemy-w-pyodbc-freetds-ubuntu/

and of course RTFM: http://www.freetds.org/userguide/

Can you narrow down the problem by using isql mydsn or tsql -S mydsn from the command line? Pretty sure this will tell you that your problem is in the FreeTDS setup.

It looks like your odbcinst.ini has a few diffs from mine:

[TDSDriver]
Description = TDS driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

My freetds.conf is the same except tds version = 8.0 and the name of my server entry is the same as it's entry in odbc.ini.

This and the docs should hopefully get you there.