SlideShare a Scribd company logo
1 of 26
www.r-
R2 Academy RMySQL Tutorial
Access MySQL from R
Course Material R2 Academy
All the material related to this course are available at our Website
Slides can be viewed at SlideShare
Scripts can be downloaded from GitHub
Videos can be viewed on our YouTube Channel
www.rsquaredacademy.com 2
Table Of Contents R2 Academy
→ Objectives
→ Introduction
→ Installing RMySQL
→ RMySQL Commands
→ Connecting to MySQL
→ Database Info
→ Listing Tables
→ Creating Tables
→ Import data into R data frame
→ Export data from R
www.rsquaredacademy.com 3
Objectives R2 Academy
→ Install & load RMySQL package
→ Connect to a MySQL Database from R
→ Display database information
→ List tables in the database
→ Create new table
→ Import data into R for analysis
→ Export data from R
→ Remove tables & disconnect
www.rsquaredacademy.com 4
Introduction R2 Academy
www.rsquaredacademy.com 5
In real world, data is often stored in relational databases such as MySQL and an analyst is required to extract the
data in order to perform any type of analysis. If you are using R for statistical analysis and a relational database for
storing the data, you need to interact with the database in order to access the relevant data sets.
One way to accomplish the above task is to export the data from the database in some file format and import the
same into R. Similarly, if you have some data as a data frame in R and want to store it in a database, you will need to
export the data from R and import it into the database. This method can be very cumbersome and frustrating.
The RMySQL package was created to help R users to easily access a MySQL database from R. In order to take
advantage of the features of the package, you need the following:
• Access to MySQL database
• Knowledge of basic SQL commands
• Latest version of R (3.2.3)
• RStudio (Version 0.99.491) (Optional)
• RMySQL Package (Version 0.10.8)
RMySQL Package R2 Academy
www.rsquaredacademy.com 6
RMySQL package allows you to access MySQL from R. It was created by Jeffrey Horner but is being maintained by
Jeroen Ooms and Hadley Wickham. The latest release of the package is version 0.10.8. You can install and load the
package using the following commands:
# install the package
install.packages("RMySQL")
# load the package
library(RMySQL)
Connect To Database R2 Academy
www.rsquaredacademy.com 7
We can establish a connection to a MySQL database using the dbConnect() function. In order to connect to the
database, we need to specify the following:
• MySQL Connection
• Database name
• Username
• Password
• Host Details
Below is an example:
# create a MySQL connection object
con <- dbConnect(MySQL(),
user = 'root',
password = 'password',
host = 'localhost',
dbname = 'world')
Connection Summary R2 Academy
www.rsquaredacademy.com 8
We can get a summary or meta data of the connection using summary() function. We need to specify the name of
the MySQL Connection object for which we are seeking meta data.
Below is an example:
# connect to MySQL
con <- dbConnect(MySQL(),
user = 'root',
password = 'password',
host = 'localhost',
dbname = 'world')
> summary(con)
<MySQLConnection:0,0>
User: root
Host: localhost
Dbname: world
Connection type: localhost via TCP/IP
Results:
Database Info R2 Academy
www.rsquaredacademy.com 9
The dbGetInfo() function can be used
to access information about the
database to which we have established
a connection. Among other things, it
will return the following information
about host, server and connection
type.
> dbGetInfo(con)
$host
[1] "localhost"
$user
[1] "root"
$dbname
[1] "world"
$conType
[1] "localhost via TCP/IP"
$serverVersion
[1] "5.7.9-log"
$protocolVersion
[1] 10
$threadId
[1] 7
$rsId
list()
List Tables R2 Academy
www.rsquaredacademy.com 10
Once we have successfully established a connection to a MySQL database, we can use the dbListTables() function
to access the list of tables that are present in that particular database. We need to specify the name of the MySQL
connection object for which we are seeking the list of tables.
Below is an example:
# list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars"
As you can see, there are four tables in the database to which we established the connection through RMySQL
package. In the function, we have not specified the database name but the name of the MySQL connection object
we created when we connected to the database.
List Fields R2 Academy
www.rsquaredacademy.com 11
To get a list of fields or columns in a particular table in the database, we can use the dbListFields() function. We
need to specify the name of the MySQL connection object as well as the table name. If the table exists in the
database, the names of the fields will be returned.
Below is an example:
# list of fields in table city
> dbListFields(con, "city")
[1] "ID" "Name" "CountryCode" "District"
[5] "Population"
The name of the table must be enclosed in single/double quotes and the names of the fields is returned as a
character vector.
Testing Data Types R2 Academy
www.rsquaredacademy.com 12
To test the SQL data type of an object, we can use the dbDataType() function.
Below is an example:
> # data type
> dbDataType(RMySQL::MySQL(), "a")
[1] "text"
> dbDataType(RMySQL::MySQL(), 1:5)
[1] "bigint"
> dbDataType(RMySQL::MySQL(), 1.5)
[1] "double"
We need to specify the driver details as well as the object to test the SQL data type.
Querying Data R2 Academy
www.rsquaredacademy.com 13
There are three different methods of querying data from a database:
• Import the complete table using dbReadTable()
• Send query and retrieve results using dgGetQuery()
• Submit query using dbSendQuery() and fetch results using dbFetch()
Let us explore each of the above methods one by one.
Import Table R2 Academy
www.rsquaredacademy.com 14
The dbReadTable() can be used to extract an entire table from a MySQL database. We can use this method only if
the table is not very big. We need to specify the name of the MySQL connection object and the table. The name of
the table must be enclosed in single/double quotes.
In the below example, we read the entire table named “trial” from the database.
> dbReadTable(con, "trial")
x y
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
6 6 f
7 7 g
8 8 h
9 9 i
10 10 j
Import Rows R2 Academy
www.rsquaredacademy.com 15
The dbGetQuery() function can be used to extract specific rows from a table. We can use this method when we
want to import rows that meet certain conditions from a big table stored in the database. We need to specify the
name of the MySQL connection object and query. The query must be enclosed in single/double quotes.
In the below example, we read the first 5 lines from the table named trial.
> dbGetQuery(con, "SELECT * FROM trial LIMIT 5;")
x y
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
dbGetQuery() function sends the query and fetches the results from the table in the database.
Import Data in Batches R2 Academy
www.rsquaredacademy.com 16
We can import data in batches as well. To achieve this, we will use two different functions:
• dbSendQuery()
• dbFetch()
dbSendQuery() will submit the query but will not extract any data. To fetch data from the database, we will use
the dbFetch() function which will fetch data from the query that was executed by dbSendQuery(). As you can
see, this method works in two stages. Let us look at an example to get a better understanding:
> # pull data in batches
> query <- dbSendQuery(con, "SELECT * FROM trial;")
> data <- dbFetch(query, n = 5)
We store the result of the dbSendQuery() function in an object ‘query.’ The MySQL connection object and the
SQL query are the inputs to this function. Next, we fetch the data using the dbFetch() function. The inputs for this
function are the result of the dbSendQuery() function and the number of rows to be fetched. The rows fetched
are stored in a new object ‘data ‘.
Query Information R2 Academy
www.rsquaredacademy.com 17
The dbGetInfo() function returns information about query that has been submitted for execution using
dbSendQuery(). Below is an example:
> res <- dbSendQuery(con, "SELECT * FROM trial;")
> dbGetInfo(res)
$statement
[1] "SELECT * FROM trial;"
$isSelect
[1] 1
$rowsAffected
[1] -1
$rowCount
[1] 0
$completed
[1] 0
$fieldDescription
$fieldDescription[[1]]
NULL
Query & Rows Info R2 Academy
www.rsquaredacademy.com 18
The dbGetStatement() function returns query that has been submitted for execution using dbSendQuery().
Below is an example:
> res <- dbSendQuery(con, "SELECT * FROM trial;")
> dbGetStatement(res)
[1] "SELECT * FROM trial;“
The dbGetRowCount() function returns the number of rows fetched from the database by the dbFetch()
function. Below is an example:
> res <- dbSendQuery(con, "SELECT * FROM trial;")
> data <- dbFetch(res, n = 5)
> dbGetRowCount(res)
[1] 5
The dbGetRowsAffected() function returns the number of rows affected returns query that has been submitted
for execution using dbSendQuery() function. Below is an example:
> dbGetRowsAffected(res)
[1] -1
Column Info R2 Academy
www.rsquaredacademy.com 19
The dbColumnInfo() function returns information about the columns of the table for which query has been
submitted using dbSendQuery(). Below is an example:
> res <- dbSendQuery(con, "SELECT * FROM trial;")
> dbColumnInfo(res)
name Sclass type length
1 row_names character BLOB/TEXT 196605
2 x double BIGINT 20
3 y character BLOB/TEXT 196605
The dbClearResult() function frees all the resources associated with the result set of the dbSendQuery()
function. Below is an example:
> res <- dbSendQuery(con, "SELECT * FROM trial;")
> dbClearResult(res)
[1] TRUE
Export/Write Table R2 Academy
www.rsquaredacademy.com 20
The dbWriteTable() function is used to export data from R to a database. It can be used for the following:
• Create new table
• Overwrite existing table
• Append data to table
In the first example, we will create a dummy data set and export it to the database. We will specify the following
within the dbWriteTable() function:
1. Name of the MySQL connection object
2. Name of the table to created in the database
3. Name of the data frame to be exported
Export/Write Table R2 Academy
www.rsquaredacademy.com 21
We will create the table trial that we have so far used in all the previous examples:
# list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars"
# create dummy data set
> x <- 1:10
> y <- letters[1:10]
> trial <- data.frame(x, y, stringsAsFactors = FALSE)
# create table in the database
> dbWriteTable(con, "trial", trial)
[1] TRUE
# updated list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars" "trial"
Overwrite Table R2 Academy
www.rsquaredacademy.com 22
We can overwrite the data in a table by using the overwrite option and setting it to TRUE. Let us overwrite the
table we created in the previous example:
# list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars" "trial"
# create dummy data set
> x <- sample(100, 10)
> y <- letters[11:20]
> trial2 <- data.frame(x, y, stringsAsFactors = FALSE)
# overwrite table in the database
> dbWriteTable(con, "trial", trial2, overwrite = TRUE)
[1] TRUE
Append Data R2 Academy
www.rsquaredacademy.com 23
We can overwrite the data in a table by using the append option and setting it to TRUE. Let us append data to the
table we created in the previous example:
# list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars" "trial"
# create dummy data set
> x <- sample(100, 10)
> y <- letters[5:14]
> trial3 <- data.frame(x, y, stringsAsFactors = FALSE)
# append data to the table in the database
> dbWriteTable(con, "trial", trial3, append = TRUE)
[1] TRUE
Remove Table R2 Academy
www.rsquaredacademy.com 24
The dbRemoveTable() function can be used to remove tables from the database. We need to specify the name of
the MySQL connection object and the table to be removed. The name of the table must be enclosed in single/double
quotes. Below is an example
# list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars" "trial"
# remove table trial
> dbRemoveTable(con, "trial")
[1] TRUE
# updated list of tables in the database
> dbListTables(con)
[1] "city" "country" "countrylanguage"
[4] "mtcars"
Disconnect R2 Academy
www.rsquaredacademy.com 25
It is very important to close the connection to the database. The dbDisconnect() function can be used to
disconnect from the database. We need to specify the name of the MySQL connection object. Below is an example
# create a MySQL connection object
con <- dbConnect(MySQL(),
user = 'root',
password = 'password',
host = 'localhost',
dbname = 'world')
# disconnect from the database
> dbDisconnect(con)
[1] TRUE
R2 Academy
www.rsquaredacademy.com 26
Visit Rsquared Academy for
tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub

More Related Content

What's hot

Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining Sulman Ahmed
 
Introduction to Cassandra Architecture
Introduction to Cassandra ArchitectureIntroduction to Cassandra Architecture
Introduction to Cassandra Architecturenickmbailey
 
Data Warehouse Modeling
Data Warehouse ModelingData Warehouse Modeling
Data Warehouse Modelingvivekjv
 
SQL for Data Science
SQL for Data ScienceSQL for Data Science
SQL for Data ScienceWeCloudData
 
Database security
Database securityDatabase security
Database securityBirju Tank
 
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자PgDay.Seoul
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsSpringPeople
 
Data science life cycle
Data science life cycleData science life cycle
Data science life cycleManoj Mishra
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Deepam Aggarwal
 
MYSQL Aggregate Functions
MYSQL Aggregate FunctionsMYSQL Aggregate Functions
MYSQL Aggregate FunctionsLeroy Blair
 
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and PrivacyClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and PrivacyAltinity Ltd
 
Introduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLIntroduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLHarmony Kwawu
 

What's hot (20)

Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining
 
Sql commands
Sql commandsSql commands
Sql commands
 
Introduction to Cassandra Architecture
Introduction to Cassandra ArchitectureIntroduction to Cassandra Architecture
Introduction to Cassandra Architecture
 
Data Warehouse Modeling
Data Warehouse ModelingData Warehouse Modeling
Data Warehouse Modeling
 
SQL
SQLSQL
SQL
 
SQL for Data Science
SQL for Data ScienceSQL for Data Science
SQL for Data Science
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Database security
Database securityDatabase security
Database security
 
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Data science life cycle
Data science life cycleData science life cycle
Data science life cycle
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
MYSQL Aggregate Functions
MYSQL Aggregate FunctionsMYSQL Aggregate Functions
MYSQL Aggregate Functions
 
Sql commands
Sql commandsSql commands
Sql commands
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and PrivacyClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
ClickHouse Defense Against the Dark Arts - Intro to Security and Privacy
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Introduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQLIntroduction to the Structured Query Language SQL
Introduction to the Structured Query Language SQL
 

Viewers also liked

R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersRsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsRsquared Academy
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First StepsRsquared Academy
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In RRsquared Academy
 
Database Project Airport management System
Database Project Airport management SystemDatabase Project Airport management System
Database Project Airport management SystemFahad Chishti
 
R Programming: Export/Output Data In R
R Programming: Export/Output Data In RR Programming: Export/Output Data In R
R Programming: Export/Output Data In RRsquared Academy
 
No SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBNo SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBKen Cenerelli
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: IntroductionRsquared Academy
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeRsquared Academy
 
R Journal 2009 1
R Journal 2009 1R Journal 2009 1
R Journal 2009 1Ajay Ohri
 
Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2rusersla
 
Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2rusersla
 
Accessing Databases from R
Accessing Databases from RAccessing Databases from R
Accessing Databases from Rkmettler
 
Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RYogesh Khandelwal
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data TypesRsquared Academy
 
Self Learning Credit Scoring Model Presentation
Self Learning Credit Scoring Model PresentationSelf Learning Credit Scoring Model Presentation
Self Learning Credit Scoring Model PresentationSwitchPitch
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudioRsquared Academy
 

Viewers also liked (20)

R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First Steps
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
Database Project Airport management System
Database Project Airport management SystemDatabase Project Airport management System
Database Project Airport management System
 
R Programming: Export/Output Data In R
R Programming: Export/Output Data In RR Programming: Export/Output Data In R
R Programming: Export/Output Data In R
 
No SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBNo SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDB
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
 
R Journal 2009 1
R Journal 2009 1R Journal 2009 1
R Journal 2009 1
 
Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2
 
Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2
 
Accessing Databases from R
Accessing Databases from RAccessing Databases from R
Accessing Databases from R
 
Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using R
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
Self Learning Credit Scoring Model Presentation
Self Learning Credit Scoring Model PresentationSelf Learning Credit Scoring Model Presentation
Self Learning Credit Scoring Model Presentation
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudio
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter2
Chapter2Chapter2
Chapter2
 

Similar to RMySQL Tutorial For Beginners (20)

Python database access
Python database accessPython database access
Python database access
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
Lecture17
Lecture17Lecture17
Lecture17
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part II
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Jdbc
JdbcJdbc
Jdbc
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
PHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHPPHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHP
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Interfacing python to mysql (11363255151).pptx
Interfacing python to mysql (11363255151).pptxInterfacing python to mysql (11363255151).pptx
Interfacing python to mysql (11363255151).pptx
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
 
Jdbc
JdbcJdbc
Jdbc
 
MySQLi - An Improved Extension of MySQL
MySQLi - An Improved Extension of MySQLMySQLi - An Improved Extension of MySQL
MySQLi - An Improved Extension of MySQL
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
JDBC
JDBCJDBC
JDBC
 

More from Rsquared Academy

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in RRsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using RRsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with PipesRsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRsquared Academy
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in RRsquared Academy
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsRsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsRsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersRsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsRsquared Academy
 

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 

Recently uploaded

Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...amitlee9823
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsJoseMangaJr1
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...amitlee9823
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...amitlee9823
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
hybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptxhybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptx9to5mart
 

Recently uploaded (20)

Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men  🔝Thrissur🔝   Escor...
➥🔝 7737669865 🔝▻ Thrissur Call-girls in Women Seeking Men 🔝Thrissur🔝 Escor...
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
hybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptxhybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptx
 

RMySQL Tutorial For Beginners

  • 1. www.r- R2 Academy RMySQL Tutorial Access MySQL from R
  • 2. Course Material R2 Academy All the material related to this course are available at our Website Slides can be viewed at SlideShare Scripts can be downloaded from GitHub Videos can be viewed on our YouTube Channel www.rsquaredacademy.com 2
  • 3. Table Of Contents R2 Academy → Objectives → Introduction → Installing RMySQL → RMySQL Commands → Connecting to MySQL → Database Info → Listing Tables → Creating Tables → Import data into R data frame → Export data from R www.rsquaredacademy.com 3
  • 4. Objectives R2 Academy → Install & load RMySQL package → Connect to a MySQL Database from R → Display database information → List tables in the database → Create new table → Import data into R for analysis → Export data from R → Remove tables & disconnect www.rsquaredacademy.com 4
  • 5. Introduction R2 Academy www.rsquaredacademy.com 5 In real world, data is often stored in relational databases such as MySQL and an analyst is required to extract the data in order to perform any type of analysis. If you are using R for statistical analysis and a relational database for storing the data, you need to interact with the database in order to access the relevant data sets. One way to accomplish the above task is to export the data from the database in some file format and import the same into R. Similarly, if you have some data as a data frame in R and want to store it in a database, you will need to export the data from R and import it into the database. This method can be very cumbersome and frustrating. The RMySQL package was created to help R users to easily access a MySQL database from R. In order to take advantage of the features of the package, you need the following: • Access to MySQL database • Knowledge of basic SQL commands • Latest version of R (3.2.3) • RStudio (Version 0.99.491) (Optional) • RMySQL Package (Version 0.10.8)
  • 6. RMySQL Package R2 Academy www.rsquaredacademy.com 6 RMySQL package allows you to access MySQL from R. It was created by Jeffrey Horner but is being maintained by Jeroen Ooms and Hadley Wickham. The latest release of the package is version 0.10.8. You can install and load the package using the following commands: # install the package install.packages("RMySQL") # load the package library(RMySQL)
  • 7. Connect To Database R2 Academy www.rsquaredacademy.com 7 We can establish a connection to a MySQL database using the dbConnect() function. In order to connect to the database, we need to specify the following: • MySQL Connection • Database name • Username • Password • Host Details Below is an example: # create a MySQL connection object con <- dbConnect(MySQL(), user = 'root', password = 'password', host = 'localhost', dbname = 'world')
  • 8. Connection Summary R2 Academy www.rsquaredacademy.com 8 We can get a summary or meta data of the connection using summary() function. We need to specify the name of the MySQL Connection object for which we are seeking meta data. Below is an example: # connect to MySQL con <- dbConnect(MySQL(), user = 'root', password = 'password', host = 'localhost', dbname = 'world') > summary(con) <MySQLConnection:0,0> User: root Host: localhost Dbname: world Connection type: localhost via TCP/IP Results:
  • 9. Database Info R2 Academy www.rsquaredacademy.com 9 The dbGetInfo() function can be used to access information about the database to which we have established a connection. Among other things, it will return the following information about host, server and connection type. > dbGetInfo(con) $host [1] "localhost" $user [1] "root" $dbname [1] "world" $conType [1] "localhost via TCP/IP" $serverVersion [1] "5.7.9-log" $protocolVersion [1] 10 $threadId [1] 7 $rsId list()
  • 10. List Tables R2 Academy www.rsquaredacademy.com 10 Once we have successfully established a connection to a MySQL database, we can use the dbListTables() function to access the list of tables that are present in that particular database. We need to specify the name of the MySQL connection object for which we are seeking the list of tables. Below is an example: # list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" As you can see, there are four tables in the database to which we established the connection through RMySQL package. In the function, we have not specified the database name but the name of the MySQL connection object we created when we connected to the database.
  • 11. List Fields R2 Academy www.rsquaredacademy.com 11 To get a list of fields or columns in a particular table in the database, we can use the dbListFields() function. We need to specify the name of the MySQL connection object as well as the table name. If the table exists in the database, the names of the fields will be returned. Below is an example: # list of fields in table city > dbListFields(con, "city") [1] "ID" "Name" "CountryCode" "District" [5] "Population" The name of the table must be enclosed in single/double quotes and the names of the fields is returned as a character vector.
  • 12. Testing Data Types R2 Academy www.rsquaredacademy.com 12 To test the SQL data type of an object, we can use the dbDataType() function. Below is an example: > # data type > dbDataType(RMySQL::MySQL(), "a") [1] "text" > dbDataType(RMySQL::MySQL(), 1:5) [1] "bigint" > dbDataType(RMySQL::MySQL(), 1.5) [1] "double" We need to specify the driver details as well as the object to test the SQL data type.
  • 13. Querying Data R2 Academy www.rsquaredacademy.com 13 There are three different methods of querying data from a database: • Import the complete table using dbReadTable() • Send query and retrieve results using dgGetQuery() • Submit query using dbSendQuery() and fetch results using dbFetch() Let us explore each of the above methods one by one.
  • 14. Import Table R2 Academy www.rsquaredacademy.com 14 The dbReadTable() can be used to extract an entire table from a MySQL database. We can use this method only if the table is not very big. We need to specify the name of the MySQL connection object and the table. The name of the table must be enclosed in single/double quotes. In the below example, we read the entire table named “trial” from the database. > dbReadTable(con, "trial") x y 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 f 7 7 g 8 8 h 9 9 i 10 10 j
  • 15. Import Rows R2 Academy www.rsquaredacademy.com 15 The dbGetQuery() function can be used to extract specific rows from a table. We can use this method when we want to import rows that meet certain conditions from a big table stored in the database. We need to specify the name of the MySQL connection object and query. The query must be enclosed in single/double quotes. In the below example, we read the first 5 lines from the table named trial. > dbGetQuery(con, "SELECT * FROM trial LIMIT 5;") x y 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e dbGetQuery() function sends the query and fetches the results from the table in the database.
  • 16. Import Data in Batches R2 Academy www.rsquaredacademy.com 16 We can import data in batches as well. To achieve this, we will use two different functions: • dbSendQuery() • dbFetch() dbSendQuery() will submit the query but will not extract any data. To fetch data from the database, we will use the dbFetch() function which will fetch data from the query that was executed by dbSendQuery(). As you can see, this method works in two stages. Let us look at an example to get a better understanding: > # pull data in batches > query <- dbSendQuery(con, "SELECT * FROM trial;") > data <- dbFetch(query, n = 5) We store the result of the dbSendQuery() function in an object ‘query.’ The MySQL connection object and the SQL query are the inputs to this function. Next, we fetch the data using the dbFetch() function. The inputs for this function are the result of the dbSendQuery() function and the number of rows to be fetched. The rows fetched are stored in a new object ‘data ‘.
  • 17. Query Information R2 Academy www.rsquaredacademy.com 17 The dbGetInfo() function returns information about query that has been submitted for execution using dbSendQuery(). Below is an example: > res <- dbSendQuery(con, "SELECT * FROM trial;") > dbGetInfo(res) $statement [1] "SELECT * FROM trial;" $isSelect [1] 1 $rowsAffected [1] -1 $rowCount [1] 0 $completed [1] 0 $fieldDescription $fieldDescription[[1]] NULL
  • 18. Query & Rows Info R2 Academy www.rsquaredacademy.com 18 The dbGetStatement() function returns query that has been submitted for execution using dbSendQuery(). Below is an example: > res <- dbSendQuery(con, "SELECT * FROM trial;") > dbGetStatement(res) [1] "SELECT * FROM trial;“ The dbGetRowCount() function returns the number of rows fetched from the database by the dbFetch() function. Below is an example: > res <- dbSendQuery(con, "SELECT * FROM trial;") > data <- dbFetch(res, n = 5) > dbGetRowCount(res) [1] 5 The dbGetRowsAffected() function returns the number of rows affected returns query that has been submitted for execution using dbSendQuery() function. Below is an example: > dbGetRowsAffected(res) [1] -1
  • 19. Column Info R2 Academy www.rsquaredacademy.com 19 The dbColumnInfo() function returns information about the columns of the table for which query has been submitted using dbSendQuery(). Below is an example: > res <- dbSendQuery(con, "SELECT * FROM trial;") > dbColumnInfo(res) name Sclass type length 1 row_names character BLOB/TEXT 196605 2 x double BIGINT 20 3 y character BLOB/TEXT 196605 The dbClearResult() function frees all the resources associated with the result set of the dbSendQuery() function. Below is an example: > res <- dbSendQuery(con, "SELECT * FROM trial;") > dbClearResult(res) [1] TRUE
  • 20. Export/Write Table R2 Academy www.rsquaredacademy.com 20 The dbWriteTable() function is used to export data from R to a database. It can be used for the following: • Create new table • Overwrite existing table • Append data to table In the first example, we will create a dummy data set and export it to the database. We will specify the following within the dbWriteTable() function: 1. Name of the MySQL connection object 2. Name of the table to created in the database 3. Name of the data frame to be exported
  • 21. Export/Write Table R2 Academy www.rsquaredacademy.com 21 We will create the table trial that we have so far used in all the previous examples: # list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" # create dummy data set > x <- 1:10 > y <- letters[1:10] > trial <- data.frame(x, y, stringsAsFactors = FALSE) # create table in the database > dbWriteTable(con, "trial", trial) [1] TRUE # updated list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" "trial"
  • 22. Overwrite Table R2 Academy www.rsquaredacademy.com 22 We can overwrite the data in a table by using the overwrite option and setting it to TRUE. Let us overwrite the table we created in the previous example: # list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" "trial" # create dummy data set > x <- sample(100, 10) > y <- letters[11:20] > trial2 <- data.frame(x, y, stringsAsFactors = FALSE) # overwrite table in the database > dbWriteTable(con, "trial", trial2, overwrite = TRUE) [1] TRUE
  • 23. Append Data R2 Academy www.rsquaredacademy.com 23 We can overwrite the data in a table by using the append option and setting it to TRUE. Let us append data to the table we created in the previous example: # list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" "trial" # create dummy data set > x <- sample(100, 10) > y <- letters[5:14] > trial3 <- data.frame(x, y, stringsAsFactors = FALSE) # append data to the table in the database > dbWriteTable(con, "trial", trial3, append = TRUE) [1] TRUE
  • 24. Remove Table R2 Academy www.rsquaredacademy.com 24 The dbRemoveTable() function can be used to remove tables from the database. We need to specify the name of the MySQL connection object and the table to be removed. The name of the table must be enclosed in single/double quotes. Below is an example # list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars" "trial" # remove table trial > dbRemoveTable(con, "trial") [1] TRUE # updated list of tables in the database > dbListTables(con) [1] "city" "country" "countrylanguage" [4] "mtcars"
  • 25. Disconnect R2 Academy www.rsquaredacademy.com 25 It is very important to close the connection to the database. The dbDisconnect() function can be used to disconnect from the database. We need to specify the name of the MySQL connection object. Below is an example # create a MySQL connection object con <- dbConnect(MySQL(), user = 'root', password = 'password', host = 'localhost', dbname = 'world') # disconnect from the database > dbDisconnect(con) [1] TRUE
  • 26. R2 Academy www.rsquaredacademy.com 26 Visit Rsquared Academy for tutorials on: → R Programming → Business Analytics → Data Visualization → Web Applications → Package Development → Git & GitHub