R and MySQL Database Tutorial
library(RMySQL)
# Hello visitors, this tutorials is to connect and working with mysql server using RMySQl package
# Although, it is bit tricky and difficult to instal this package on lastest version on R, but you should do it before using this tutorial
# If in case, you find it difficult to instal RMySQl on your computer, please send me a message, so that I can develop a tutorials for that as well
library(RMySQL)
# mydb = dbConnect(MySQL(), user='manoj', password='password', host='localhost')
# creating a database using RMySQL in R
# dbSendQuery(mydb, "CREATE DATABASE bookstore;")
# dbSendQuery(mydb, "USE bookstore")
# reconnecting to database we just created using following command in R :
mydb = dbConnect(MySQL(), user='manoj', password='password', host='localhost', dbname="bookstore")
dbSendQuery(mydb, "drop table if exists books, authors")
# creating tables in bookstore:
dbSendQuery(mydb, "
CREATE TABLE books (
book_id INT,
title VARCHAR(50),
author VARCHAR(50));")
# Show table using R:
dbListTables(mydb)
## [1] "books"
# Considering our bookstore a bit more, we realize that we need to add a few more columns for data elements: publisher, publication year, ISBN number, genre (e.g., novel, poetry, drama), description of book, etc.
# We also realize that we want MySQL to automatically assign a number to the book_id column so that we don't have to bother creating one for each row or worry about duplicates.
# Additionally, we've decided to change the author column from the actual author's name to an identification number that we'll join to a separate table containing a list of authors.
# This will reduce typing, and will make sorting and searching easier, as the data will be uniform.
# To make these alterations to the table that we've already created, enter the following SQL command through R :
dbSendQuery(mydb, "ALTER TABLE books
CHANGE COLUMN book_id book_id INT AUTO_INCREMENT PRIMARY KEY,
CHANGE COLUMN author author_id INT,
ADD COLUMN description TEXT,
ADD COLUMN genre ENUM('novel','poetry','drama', 'tutorials', 'text', 'other'),
ADD COLUMN publisher_id INT,
ADD COLUMN pub_year VARCHAR(4),
ADD COLUMN isbn VARCHAR(20);")
# if R gives you an error:
# Error in mysqlExecStatement(conn, statement, ...) :
# RS-DBI driver: (connection with pending rows, close resultSet before continuing)
# reconnect database using:
mydb = dbConnect(MySQL(), user='manoj', password='password', host='localhost', dbname="bookstore")
# and then run the above command of table alteration
# Now, Before moving on to adding data to our books table, let's quickly set up the authors table.
dbSendQuery(mydb, "CREATE TABLE authors
(author_id INT AUTO_INCREMENT PRIMARY KEY,
author_last VARCHAR(50),
author_first VARCHAR(50),
country VARCHAR(50));")
# Adding data into tables
dbSendQuery(mydb, "INSERT INTO authors
(author_last, author_first, country)
VALUES('Kumar','Manoj','India');")
# fetching last data insert id number:
last_id = fetch(dbSendQuery(mydb, "SELECT LAST_INSERT_ID();"))
# Inserting data into books table and using last insert ID number:
dbSendQuery(mydb, "INSERT INTO books
(title, author_id, isbn, genre, pub_year)
VALUES('R and MySQL', 1,'6900690075','tutorials','2014');")
# Note that I have not provided publisher's id and description in the above data insert, if you want you can modify that... at least try!
# Also note that we just entered for the author by using the LAST_INSERT_ID() function. you should also try:
# SELECT author_id, author_first FROM authors;
# and insert data using that....
try1 = fetch(dbSendQuery(mydb, "SELECT book_id, title, description
FROM books
WHERE genre = 'tutorials';"))
# That's all for now... will come back soon with some advanced tutorials on R and MySQL
Subi said:
Hello Prof. Kumar,
I am a total newbie in R I have a wordpress website set up in Wamp server with a MySQL database as well placed in Wamp. I need to fetch data from database and apply anlytics on it using R script. Can you please help me set up R on WAMP ASAP.
PS: Project due on the 5th of December 2014
LikeLiked by 1 person
MANOJ KUMAR said:
Hi in R if you are planning to fetch data from WAMP server, you can do it by establishing a connection using ODBC connection. Please refer to step-by-step tutorial here:
LikeLike
amalgovinus said:
Couldn’t get RMySQL working in windows. Had to resort to RODBC.
LikeLike
@sonalkat said:
Reblogged this on TrystWithData and commented:
Connecting R to MySQL.. hope this works!!!
LikeLike
zenagiwa said:
Reblogged this on Zaynaib Giwa.
LikeLike
Matt said:
Can I use RMySQL to download data from a MySQL database hosted through a website? If so can you point me in the right direction?
I do not have permissions for the website, but there exists a GUI to export the data which is liable to timing out and crashing.
LikeLike
MANOJ KUMAR said:
yes you can… all you need is access to that MySQL database on website. all other options like the one you mentioned, GUI, will not work.
LikeLike
Rajan Sharma said:
Thank you, it worked on my system.
LikeLike
Samarth said:
Hey,
I am succesful at connecting MySQL with R. using the following code. I am also working with twitter to parse to tweets into database. My tweets are continuous in time. Using the filterStream function. I would like to parse these tweets into a database. Unfortunately, I am getting error. Any help would be appreciated.
con <- dbConnect(MySQL(),user = 'root', password = 'xxx', dbname = 'Tweets_Science', host = 'localhost')
dbListTables(con)
filterStream(file.name= dbWriteTable(con,"test"), track=c("hunting"), timeout=10, oauth=twitCred)
Error: Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbWriteTable’ for signature ‘"MySQLConnection", "character", "missing"’
Thank you
LikeLike
MANOJ KUMAR said:
Samarth, actually your code is just a part of all, i guess, little bit confusing me. if you don’t mind send me complete code, what you have written, into my email. Will revert asap.
LikeLike
Samarth said:
library(twitteR)
library(ROAuth)
library(streamR)
library(httr)
library(RMySQL)
library(psych)
library(ROAuth)
access_token<-"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token_secret<-"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
consumerKey <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
consumerSecret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
setup_twitter_oauth(consumerKey, consumerSecret, access_token, access_token_secret)
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
con <- dbConnect(MySQL(),user = 'root', password = 'eutrophication', dbname = 'Tweets_Science', host = 'localhost')
dbListTables(con)
filterStream(file.name= dbWriteTable(con,"test"), track=c("dieoff","endangered","Peary caribou","moose","Woodland caribou","muskox","musk ox","ice melt","arctic","climate change","hunting"), timeout=10, oauth=twitCred)
I am also tried out the following code
dbWriteTable(con,"test") <- filterStream(file.name= "", track=c("dieoff","endangered","Peary caribou","moose","Woodland caribou","muskox","musk ox","ice melt","arctic","climate change","hunting"), timeout=10, oauth=twitCred)
LikeLike
Samarth said:
I think parsing the tweet is good for cleaning the function.
For example:
parseTweets(filterStream(file.name= “”, track=c(“hunting”), timeout=10, oauth=twitCred))
LikeLike
Samarth said:
I think parsing the tweet is good for cleaning the data.
For example:
parseTweets(filterStream(file.name= “”, track=c(“hunting”), timeout=10, oauth=twitCred))
LikeLike
samarth said:
install.packages(“twitteR”)
library(twitteR)
install.packages(“streamR”)
library(streamR)
install.packages(“ROAuth”)
library(ROAuth)
install.packages(“streamR”)
library(streamR)
install.packages(“RMySQL”)
library(RMySQL)
consumer_key<- "xxxxxxxxxxxxxxx"
consumer_secret<- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token<- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token_secret<-"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
handle<- "real_samarth"
request_URL <- "https://api.twitter.com/oauth/request_token"
access_URL <- "https://api.twitter.com/oauth/access_token"
auth_URL <- "http://api.twitter.com/oauth/authorize"
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_token_secret) #We connected R with twitter
my_oauth <- OAuthFactory$new(consumerKey=consumer_key,
consumerSecret=consumer_secret, requestURL=request_URL,
accessURL=access_URL, authURL=auth_URL)
my_oauth$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
mydb = dbConnect(MySQL(), user='root', password='password', host='localhost', dbname="test")
parseTweets(filterStream(file.name= dbWriteTable(con,"test"), track=c("hunting"), timeout=10, oauth=twitCred))[,c("text","created_at")]
I want to make multiple columns but here I am giving an example that I want the filterstream function to make two columns (text and created_at) in MySQL database. This is my complete code so far. I would appreciate your help.
Thank you
LikeLike
Pingback: Using RMySQL from Ubuntu | DECISION STATS
Matt said:
First thank you for your posts, they are very helpful. I’m trying to automate a daily table construction and I have a table name variable that concatenates ‘stockprice’ and today’s date (gsub’ing out the separators). It works perfect while using dbWriteTable and dbReadTable but when I want to use it in the dbSendQuery arguments it breaks down (trying to add an AUTO_INCREMENT PRIMARY KEY column). I’ve read all the documentation for RMySQL and can’t find a solution. Any suggestions on where to look for a fix would be greatly appreciated.
LikeLiked by 1 person
Pingback: Unable to connect MySQL using R - BlogoSfera
Pingback: Ambiente R: análise e tratamento estatístico | Infocacto
Asit Mohapatra said:
I am getting the following error while trying to connect to mySQL database
Failed to connect to database: Error: Can’t connect to MySQL server on ‘localhost’ (0)
LikeLike
DataScientist said:
please check if mysql service has been started or not?
LikeLike
RRD said:
Hi thank you for this tuto, I want to connect to my website sql database. Actually, I didn`t know where to execute all these commands (like mydb = dbConnect)? when I execute the connect command under root, I get -bash: syntax error near unexpected token `(, when I execute it under R , I get error could not find function dbConnect
LikeLiked by 1 person
Praneeth Krishna said:
Hi all,
while trying to insert rows into a table in mysqlserver the console is throwing the error as below:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbExecute’ for signature ‘”RODBC”, “character”’
The connection to the server is perfectly fine
query = paste0(‘INSERT INTO PermutationTraining (‘,paste0(colnames(fout),collapse = ‘,’),’) VALUES (‘,paste0(fout[i,],collapse = ‘,’),’);’)
dbExecute(con, query)
pls help.. !!
LikeLike