Sunday, August 13, 2023

R Language Fetching Data From MySql Table

 


R Language How to Get Data From MySql  Table :


Step : 1

install.packages("RMySQL")

library("RMySQL")
Step : 2

Create a connection Object to MySQL database.
We will connect to the sample database named
"DB_school" that comes with MySql installation.

mysqlconnection = dbConnect(MySQL(),user='root',password='',dbname='DB_school',
                  host='localhost')
Step : 3
View List the tables available in this database.
dbListTables(mysqlconnection)
Step : 4
Query the "register" tables to get all the rows.
result = dbSendQuery(mysqlconnection, "select * from registerdb")
Step : 5

Store the result in a R data frame object. n = 5 is used to
fetch first 5 rows.

data.frame = fetch(result, n = 5)
print(data.frame)
Step : 6
We can pass any valid select query to get the result.
result = dbSendQuery(mysqlconnection, "select * from registerdb where YYYY = '2010'")
Step : 7
Fetch all the records(with n = -1) and store it as a data frame.
data.frame = fetch(result, n = -1)
print(data.frame)







No comments:

Post a Comment