MySQL Connectivity In R Programmin

# Create a connection Object to MySQL database.
# We will connect to the sampel database named "sakila" that comes with MySql installation.
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '', dbname = 'stud',
                            host = 'localhost')
# List the tables available in this database.
dbListTables(mysqlconnection)
# Query the "info" tables to get all the rows.
result = dbSendQuery(mysqlconnection, "select * from info")
# 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)
dbClearResult(dbListResults(mysqlconnection)[[1]])
result = dbSendQuery(mysqlconnection, "select * from info where name LIKE 'a%' ")
# Fetch all the records(with n = -1) and store it as a data frame.
data.frame = fetch(result, n = -1)
print(data.frame)
dbSendQuery(mysqlconnection, "update info set cpi = 10 where name = 'abc' ")

dbSendQuery(mysqlconnection, "insert into info(enr,name,cpi, sem)
   values(127, 'e', 6, 3)"
)
#data.frame = fetch(result, n = -1)
#print(data.frame)

result = dbSendQuery(mysqlconnection, "select * from info")
data.frame= fetch(result, n = -1)
print(data.frame)




dbWriteTable(mysqlconnection,'new',cars)

Post a Comment

Previous Post Next Post