Session Ready
Exercise

Step 1: Establish a connection

The first step to import data from a SQL database is creating a connection to it. As Filip explained, you need different packages depending on the database you want to connect to. All these package do this in a uniform way, as specified in the DBI package.

dbConnect() creates a connection between your R session and a SQL database. The first argument has to be a DBIdriver object, that specifies how connections are made and how data is mapped between R and the database. Specifically for MySQL databases, you can build such a driver with RMySQL::MySQL().

If the MySQL database is a remote database hosted on a server, you'll also have to specify the following arguments in dbConnect(): dbname, host, port, user and password. Most of these details have already been provided.

Instructions
100 XP
  • Load the DBI library, which is already installed on DataCamp's servers.
  • Edit the dbConnect() call to connect to the MySQL database. Change the port argument (3306) and user argument ("student").
  • Print con to the console.