Paket RMySQL merupakan salah satu paket bawaan bahasa R yang paling penting. Paket ini menyediakan konektivitas asli antara database R dan MySql. untuk bekerja dengan database MySql, pertama-tama harus menginstal paket RMySQL.
1. Install paket
install.packages("RMySQL")
2. Buat database : employee dan table : employee_info
3. Connect Database Mysql
#Loading RMySQL package into R
library("RMySQL")
# Creating a connection Object to MySQL database.
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',
host = 'localhost')
# Listing the tables available in this database.
dbListTables(mysql_connect)
4. Create table
#Loading RMySQL package into R
library("RMySQL")
# Creating a connection Object to MySQL database.
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',
host = 'localhost')
#Creating data frame to create a table
emp.data<- data.frame(
name = c("Raman","Rafia","Himanshu","jasmine","Yash"),
salary = c(623.3,915.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11","2015-03-27")),
dept = c("Operations","IT","HR","IT","Finance"),
stringsAsFactors = FALSE
)
# All the rows of emp.data are taken inot MySql.
dbWriteTable(mysql_connect, "emp", emp.data[, ], overwrite = TRUE)