1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【R】RODBC経由でSQL Serverからデータ取得。

Posted at

環境 
Windows
R 3.2.5
Microsoft R Open 3.2.5

#Windows認証を使ってRODBCでSQL Serverに接続
RからRODBCパッケージを使ってSQL Serverに接続する。

library(RODBC)
#データベース接続
setDB<-function(){
  driver<-'{SQL Server}'
  server <- 'localhost\\SQLEXPRESS' 
  database <- 'NicoNico' 
  trusted_connection <- 'yes'
  conn<-paste('DRIVER=',driver,';SERVER=',server,';DATABASE=',database,';Trusted_Connection=',trusted_connection,';',sep="")
  cnxn <- odbcDriverConnect(conn)
  return(cnxn)  
}

#SQL構文でデータ取得
返り値はdateframe。適宜date.table等に変換すると使いやすいかと。

conn <- setDB()
sqlText <- "select * from [dbo].[Hoge];"
videoData<-sqlQuery(conn, sqlText)

#結果
RからSQL Serverに接続、データを取得できた。
あとはR側で統計分析すればOK。

参考
http://www.okadajp.org/RWiki/?RODBC

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?