Temporary tables using RMySQL
-
16-06-2021 - |
Question
Is there a way to create a temporary table using the RMySQL
package? If so what is the correct way to do it? In particular I am trying to write a dataframe from my R session to the temporary table. I have several processes running in parallel and I don't want to worry about name conflicts, that's why I want to make them temporary so they are only visible to each individual session. The solution should somehow involve dbWritetable
and not dbSendQuery("create temporary table tbl;")
.
NOTE: I found some stuff on the net that suggests creating a temporary table manually using dbSendQuery(con, "create temporary table x (x int)")
and then simply overriding it with dbWriteTable()
. This does not work.
Solution
For this type of job, I would avoid reinventing the wheel and use
https://code.google.com/p/sqldf/
By default, it is for sqlite, but it also works for MySQL (which I never tried). This package is rock-solid and well documented.
OTHER TIPS
Depending on your mysql account restriction can you not do
dbSendQuery(con, "create temporary table x (x int);")
dbSendQuery(con, "drop temporary table x;")
etc..
This is actually a known issue in RMySQL. Your best bet might be to write the data to a temporary file and then construct your own LOAD DATA LOCAL INFILE
statement, using RMySQL::mysqlWriteTable
as a guide.
For bonus points, if you can patch RMySQL::mysqlWriteTable to work with tempfiles, send a pull request to the github repo.