O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using RMySQL

To use the RMySQL package, follow these steps:

  1. Load the RMySQL library, and create a connection object:
> library(RMySQL) 
> con <- dbConnect(MySQL(), dbname="recommendation",     host="127.0.0.1", port=3306, username="root",     password="root") 
  1. Save the order object into a table in the database:
> dbWriteTable(con,"orders", order) 
  1. Get all accommodations from the database table:
> # Get all rows/data using dbReadTable> dbReadTable(con,"accomodation") > # Get rows based on query using dbGetQuery
> dbGetQuery(con,"select * from accommodation limit 5")
  1. Get the accommodations grouped by location, based on their price, from the database table using a loop:
> rs <- dbSendQuery(con, "select location, max(price) from accommodation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required