16

How to write the data frame in R into MySQL?

dateTime            host    authId     sessionId      status                    action 
2012-08-22 14:58:23 foo.com 221501398 2c10b368ae23ba3        questions#instant_answers
2012-08-22 14:58:23 foo.com 221501398 22c10b368ae23                      questions#new
2012-08-22 14:58:23 foo.com 221501398 01a36f64bd3f80c                     sessions#new

I want to write the dataframe all at once to the MySQL DB table. I have used RMySql package to connect and establish the connection.

Thanks

3 Answers 3

30

Use the dbWriteTable function. It looks like this :

dbWriteTable(connection, value = data.frame, name = "MyTable", append = TRUE ) 

The function is well documented.

P.S Also look at: RMySQL dbWriteTable with field.types

Sign up to request clarification or add additional context in comments.

2 Comments

It didn't worked to me. The correct syntax is dbWriteTable(connection, "MyTable", data.frame, append = TRUE). The docs: cran.r-project.org/web/packages/RMySQL/RMySQL.pdf
Adding row.names=FALSE will help seamless transfer
7

See help(dbSendQuery) for generic update ... statements in SQL, and help(dbWriteTable) to write an entire data frame to a new table.

Comments

1

It worked for me using the command below. Note that this will append the rows in yourtable to the database named yourTableInMySQL.

library(RMySQL)
dbWriteTable(con, "yourTableinMySQL", yourtable, append = TRUE)

1 Comment

I've spent hours without success trying to run DBI's function of the same name. However, RmySQL::dbWiteTable worked immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.