Connect to MSSQL using DBI

爱⌒轻易说出口 提交于 2019-12-05 07:14:24

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...

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.

Solved. I used library RODBC. It has great functionality to connect sql and run sql queries in R.

Loading Library:

library(RODBC)

# dbDriver is connection string with userID, database name, password etc.

dbhandle <- odbcDriverConnect(dbDriver)

Running Sql query

sqlQuery(channel=dbhandle, query)

Thats It.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!