Table of contents

Using RMySQL library functions

Using RMySQL library functions

The MySQL library is not pre-installed on an RStudio environment by default. If you have data stored in a MySQL database and want to use RMySQL functions to query this data, you need to install the library.

The following code snippet shows installing the library and using some basic library functions to connect to a MySQL database and query data:

# Install the package
install.packages("RMySQL")
libaray ('RMySQL')


# Connect to a remote database with your username and password
con <- dbConnect(RMySQL::MySQL(),host="<url_to_db_server>,user="<username>",password="xxx",dbname='my_sql_db')

dbListTables(con)
dbGetQuery(con, "SELECT" * FROM customers_csv")

# Always clean up by disconnecting the database
dbDisconnect(con)

Learn more about other RMySQL library functions: Package RMySQL