Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MSSQL using DBI

Tags:

r

r-dbi

I can not connect to MSSQL using DBI package. I am trying the way shown in package itself

    m <- dbDriver("RODBC") # error

Error: could not find function "RODBC"

    # open the connection using user, passsword, etc., as
    # specified in the file \file{\$HOME/.my.cnf}
    con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))    

Any help appreciated. Thanks

like image 685
indra_patil Avatar asked Jul 21 '14 13:07

indra_patil


2 Answers

As an update to this question: RStudio have since created the odbc package (or GitHub version here) that handles ODBC connections to a number of databases through DBI. For SQL Server you use:

con <- DBI::dbConnect(odbc::odbc(),
                      driver = "SQL Server",
                      server = <serverURL>,
                      database = <databasename>,
                      uid = <username>,
                      pwd = <passwd>)

You can also set a dsn or supply a connection string.

like image 94
dougmet Avatar answered Oct 10 '22 19:10

dougmet


It looks like there used to be a RODBC driver for DBI, but not any more:

http://cran.r-project.org/src/contrib/Archive/DBI.RODBC/

A bit of tweaking has got this to install in a version 3 R but I don't have any ODBC sources to test it on. But m = dbDriver("RODBC") doesn't error.

> m = dbDriver("RODBC")
> m
<ODBCDriver:(29781)> 
> 

Suggest you ask on the R-sig-db mailing list to maybe find out what happened to this code and/or the author...

like image 1
Spacedman Avatar answered Oct 10 '22 20:10

Spacedman