简体   繁体   中英

Connect R to SQL Server in MAC

I want to connect R to SQL Server on my MAC (El Capitan), I can do it very easy in Python, but in RI can't do it.

In Python it is easy as:

import pymssql
pymssql.connect(server = 'CHWN-DSX-DB02', user = 'XXXX',password ='XXXX',database = 'Info')

For R, I tried with RODBC library, but didn't work, I think the problem is the "Driver":

driver.name <- "SQL Server"
db.name <- "Info"
host.name <- "CHWN-DSX-DB02"
port <-""
server.name <-"XXX"
pwd <- "XXX"
# Use a full connection string to connect to a SAMPLE database
con.text <- paste("DRIVER=",RMySQL::MySQL(),
                  ";Database=",db.name,
                  ";Server=",host.name,
                  ";Port=",port,
                  ";PROTOCOL=TCPIP",
                  ";UID=", server.name,
                  ";PWD=",pwd,sep="")

con1 <- odbcDriverConnect(con.text)

This code in R never ends, and when I stop it, I have this warning:

Warning messages:
1: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state 00000, code 0, message [iODBC][Driver Manager]dlopen(SQL Server, 6): image not found

There are several issues with your current setup:

  1. General vs. Specific APIs : In Python you are using a specific SQL Server API: pymssql . In fair comparison to your R attempt, you should be using the ODBC API: pyodbc (to compare with RODBC ). Remember there are multiple ways to connect to backend databases or data stores from client applications either by specific APIs or generalized APIs (ODBC, OLEDB, JDBC, etc.).

  2. Required Drivers : To use any ODBC library be it Python, R, or other language, you need to have an installed ODBC driver on your client machine. Download such drivers before attempting connection. Nearly every RDBMS or data store maintains (usually free to download) ODBC drivers for Windows, Mac, and Linux OS's including SQL Server: 2013 or 2017 .

  3. Mixing R Libraries : In R, most APIs follow the DBI standard including ROracle, RJDBC, odbc, RMySQL, RPostgreSQL, RSQLite . Unfortunately, RODBC does not follow this standard. Your attempted connection appears to be an attempted DBI connection using the RMySQL::MySQL() object which even in DBI is not part of an ODBC connection string.

    Note even though both require underlying ODBC drivers (see #2), RODBC is a different library and implementation than odbc . Additionally, do not conflate specific APIs such as RMySQL with an attempted general SQL Server ODBC. In fact, it is unclear why you use RMySQL when you have a variable for driver.name . Also such a value, 'SQL Server' is the Windows ODBC driver name and not macOS or Linux names. See below R ODBC connections:

    RODBC

     driver.name <- "ODBC Driver 13 for SQL Server" # REQUIRES DOWNLOAD driver.name <- "ODBC Driver 17 for SQL Server" # REQUIRES DOWNLOAD ... con.text <- paste0("driver=", driver.name, ";database=", db.name, ";server=", host.name, ";port=", port, ";protocol=TCPIP", ";UID=", user.name, ";PWD=", pwd) conn <- odbcDriverConnect(con.text) 

    odbc

     conn <- dbConnect(odbc::odbc(), driver = driver.name, server = host.name, port = port, database = db.name uid = user.name, pwd = pwd) # ALTERNATIVE: conn <- dbConnect(odbc::odbc(), .connection_string = con.text) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM