This document summarizes how to connect R to a MySQL database. It outlines installing MySQL and MySQL Workbench, creating a database connection in R using RMySQL, executing SQL commands like CREATE DATABASE and CREATE TABLE in R to set up the database structure, and performing queries in R like INSERT, SELECT, and DROP TABLE. Key steps include establishing a connection from R to MySQL, sending queries to create and populate tables, and fetching results of queries back into R for analysis.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views
DB Connectivity and Operations With R
This document summarizes how to connect R to a MySQL database. It outlines installing MySQL and MySQL Workbench, creating a database connection in R using RMySQL, executing SQL commands like CREATE DATABASE and CREATE TABLE in R to set up the database structure, and performing queries in R like INSERT, SELECT, and DROP TABLE. Key steps include establishing a connection from R to MySQL, sending queries to create and populate tables, and fetching results of queries back into R for analysis.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15
DB connectivity & Operation
with R and MySQL
Install MySQL • Go to url http://dev.mysql.com/downloads/mysql/ Start / Stop Server • Start MySQL server >C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld • Stop MySQL Server >C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" -u root shutdown Install MySQL workbench (Optional) • Go to url h • ttps://dev.mysql.com/downloads/workbench/ Set up a connection in Workbench • To add a connection, click the [+] icon to the right of the MySQL Connections title on the Home screen. This opens the Setup New Connection form: https://dev.mysql.com/doc/workbench/en/wb-getting-started-tutorial-creating-a-model.html R and MySQL Database #Import and attach package – library(RMySQL) #Establish Connection. – mydb = dbConnect(MySQL(), user=‘user1', password='password', host='localhost') # creating a database using RMySQL in R – dbSendQuery(mydb, "CREATE DATABASE bookstore;") #Use created DB – dbSendQuery(mydb, "USE bookstore") # reconnecting to database we just created using following command in R : -- mydb = dbConnect(MySQL(), user=user1', password='password', host='localhost', dbname="bookstore") # Show table using R: dbListTables(mydb) # creating tables in bookstore: – dbSendQuery(mydb, " CREATE TABLE books ( book_id INT, title VARCHAR(50), author VARCHAR(50));") # Alter Table – 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);") • # 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,'6900690 # fetching last data insert id number: -- last_id = fetch(dbSendQuery(mydb, "SELECT LAST_INSERT_ID();"))075','tutorials','2014');") -- try1 = fetch(dbSendQuery(mydb, "SELECT book_id, title, description FROM books WHERE genre = 'tutorials';")) # Drop Table – dbSendQuery(mydb, "drop table if exists books, authors“ # Write Query for Delete by yourself