在 Ubuntu 中使用 R 在 ODBC 上连接到 Microsoft SQL Server

use*_*276 5 sql-server unixodbc freetds rodbc ubuntu-16.04

我能够让它在 Windows 7 上完全运行,但我必须将它移到 Linux 服务器上。我现在正在 VM 中运行 Ubuntu 16.04.1 LTS 64 位以进行测试,然后再在服务器上复制该过程。

我很确定问题出在我的 FreeTDS 或 DSN 设置中。
我应该停止浪费时间而只使用 Python 吗?或者我会在 ODBC 和 unixODBC 上遇到同样的问题吗?

完整的 R 脚本:

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)
Run Code Online (Sandbox Code Playgroud)

控制台

> conn <- odbcDriverConnect('myDSN')
Run Code Online (Sandbox Code Playgroud)

返回错误信息:

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
Run Code Online (Sandbox Code Playgroud)

这是我的 ODBC 和 FreeTDS 配置:

/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
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

blo*_*rth 1

我有 RODBC -> FreeTDS -> SQL Server 在 linux Mint 上工作。我配置它已经有一段时间了,但这里有一些(未经测试的)建议:

有一些很好的安装说明: http://www.saltycrane.com/blog/2011/09/notes-sqlalchemy-w-pyodbc-freetds-ubuntu/

当然还有 RTFM: http: //www.freetds.org/userguide/

isql mydsn您可以通过使用或tsql -S mydsn从命令行来缩小问题范围吗?很肯定这会告诉您您的问题出在 FreeTDS 设置中。

看起来你的 odbcinst.ini 与我的有一些差异:

[TDSDriver]
Description = TDS driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Run Code Online (Sandbox Code Playgroud)

我的 freetds.conf 是相同的,除了tds version = 8.0我的服务器条目的名称与 odbc.ini 中的条目相同。

希望这篇文章和文档可以帮助您实现目标。