R provides many convient ways to read data from files into memory. One common way is to read data from a MySQL Database. In this article, we will learn how to read data from Excel into R.
To read from a MySQL databse, we first need to connect to the database using the dbConnect
method from the RMySQL
library.
library(RMySQL)
con = dbConnect(
drv = RMySQL::MySQL(),
dbname = "cars",
host = "localhost",
username = "user",
password = "pass"
)
The above example creates a connection to a local mysql server and to the cars database. We can the send queries to the database to get rows.
sql = "SELECT * FROM cars WHERE Type = 'Electric'"
rows = dbGetQuery(con, sql)
print(rows)