LoginSignup
5
6

More than 5 years have passed since last update.

R言語でDB接続(MySQL)から取得までメモ

Last updated at Posted at 2016-07-25

R言語でDB接続(MySQL)から取得まで

# ライブラリの読み込み 
library("RMySQL")

# RStudioの場合 packagesのパネルからチェックを入れれば自動で読み込まれる
library("RMySQL", lib.loc="/usr/local/Cellar/r/3.2.2/R.framework/Versions/3.2/Resources/library")

# MySQLドライバ
md <- dbDriver("MySQL")

# DB接続
dbconnector <- dbConnect(md, dbname="<DB名>", user="<user>", password="<password>",  host="<host>") 


# dbGetQuery、dbSendQueryの違い
# クエリ実行 結果をデータセットに保管
data <- dbGetQuery(dbconnector, "select * from sample_table")

# MySQLResult形式で保管
sqlResult <- dbSendQuery(dbconnector, "select * from sample_table")

# MySQLResultからデータセット保管
data <- fetch(sqlResult)


# 破棄する
dbDisconnect(dbconnector)


5
6
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
5
6