R MySQL – Insert Data (Data Frames)

Introduction

Inserting data from data frames into a MySQL database is a common task in data analysis workflows. The RMySQL package provides functions to connect to MySQL databases and execute SQL queries, making it easy to insert data from R data frames into MySQL tables.

Installing and Loading the RMySQL Package

First, you need to install and load the RMySQL package. You can install it from CRAN using the install.packages() function.

Installing the Package

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

Loading the Package

# Load the RMySQL package
library(RMySQL)

Establishing a Connection

To establish a connection to a MySQL database, you need the following information:

  • Hostname or IP address of the MySQL server
  • Username
  • Password
  • Database name

Example: Establishing a Connection

Example:

# Establishing a connection to the MySQL database
conn <- dbConnect(RMySQL::MySQL(),
                  dbname = "your_database_name",
                  host = "your_host_name",
                  port = 3306,  # Default MySQL port
                  user = "your_username",
                  password = "your_password")

# Check if the connection is successful
print(conn)

Creating a Table

Before inserting data, you need to create a table in the MySQL database if it doesn’t already exist.

Example: Creating a Table

Example:

# SQL query to create a table
create_table_query <- "
CREATE TABLE IF NOT EXISTS employees (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  age INT NOT NULL,
  gender CHAR(1) NOT NULL,
  salary DECIMAL(10, 2) NOT NULL
);
"

# Executing the query to create the table
dbSendQuery(conn, create_table_query)
print("Table 'employees' created successfully")

Inserting Data from a Data Frame

You can insert data from a data frame into a MySQL table using the dbWriteTable() function. This function writes a data frame to a specified table in the database.

Example: Inserting Data from a Data Frame

Example:

# Creating a sample data frame
data_to_insert <- data.frame(
  name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  age = c(30, 25, 35, 28, 40),
  gender = c("F", "M", "M", "F", "F"),
  salary = c(50000, 45000, 55000, 48000, 60000)
)

# Writing the data frame to the MySQL table
dbWriteTable(conn, name = "employees", value = data_to_insert, row.names = FALSE, append = TRUE)
print("Data frame inserted into 'employees' table successfully")

Example Program Using MySQL Connection to Insert Data Frames

Here is an example program that demonstrates the complete process of connecting to a MySQL database, creating a table, inserting data from a data frame, and closing the connection.

Example Program

# R Program to Demonstrate Inserting Data Frames into MySQL Database

# Install and load the necessary package
install.packages("RMySQL")
library(RMySQL)

# Establishing a connection to the MySQL database
conn <- dbConnect(RMySQL::MySQL(),
                  dbname = "your_database_name",
                  host = "your_host_name",
                  port = 3306,  # Default MySQL port
                  user = "your_username",
                  password = "your_password")

# Check if the connection is successful
print("Connection established:")
print(conn)

# SQL query to create a table
create_table_query <- "
CREATE TABLE IF NOT EXISTS employees (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  age INT NOT NULL,
  gender CHAR(1) NOT NULL,
  salary DECIMAL(10, 2) NOT NULL
);
"

# Executing the query to create the table
dbSendQuery(conn, create_table_query)
print("Table 'employees' created successfully")

# Creating a sample data frame
data_to_insert <- data.frame(
  name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
  age = c(30, 25, 35, 28, 40),
  gender = c("F", "M", "M", "F", "F"),
  salary = c(50000, 45000, 55000, 48000, 60000)
)

# Writing the data frame to the MySQL table
dbWriteTable(conn, name = "employees", value = data_to_insert, row.names = FALSE, append = TRUE)
print("Data frame inserted into 'employees' table successfully")

# Closing the connection
dbDisconnect(conn)
print("Connection closed")

Conclusion

In this chapter, you learned how to insert data from data frames into a MySQL database from R using the RMySQL package. You also learned how to create a table and close the database connection. By mastering these functions, you can efficiently manage and insert data into MySQL databases within your R programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top