Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Why does dbWriteTable fail when table exists despite 'append = TRUE'

Tags:

mysql

r

I'm trying to append new data to a MySQL table that already exists using the dbWriteTable method. I've used it in the past without issue however it is now failing because the table already exists. This is despite using overwrite=FALSE, append=TRUE,

The code:

full_sum_table <- 'mydb.summary'    
dbWriteTable(conn=open_connection, name=full_sum_table, value=summary_data_final, overwrite=FALSE, append=TRUE, row.names=0)

Error in mysqlExecStatement(conn, statement, ...) : 
   RS-DBI driver: (could not run statement: Table 'summary' already exists)
[1] FALSE
Warning message:
In mysqlWriteTable(conn, name, value, ...) :
  could not create table: aborting mysqlWriteTable

The first call of this code works fine, but subsequent calls fail.

Any ideas would be greatly appreciated. Thanks

Environment:

  R version 3.0.2
  Packages: DBI (I was using RMySQL but it is not available for 3.0.2)
  MySQL v5.6.14
  OS: Windows Server 7
like image 860
getting-there Avatar asked Feb 05 '14 04:02

getting-there


People also ask

What happens if append is true in dbwritetable?

Alternatively, if append is TRUE, dbWriteTable () could just assume that the table exists, and an error message is given at insert time if it does not. I think this is a clean solution. If the user gives append=TRUE, then (s)he probably expects that the table already exists.

What is the return type of dbwritetable ()?

a data.frame (or coercible to data.frame). ... Other parameters passed on to methods. dbWriteTable () returns TRUE, invisibly. If the table exists, and both append and overwrite arguments are unset, or append = TRUE and the data frame with the new data has different column names, an error is raised; the remote table remains unchanged.

Why dbwritetable fails when trying to create a table from a frame?

RMySQL checks if the table already exists. This check ignores temporary tables, name of course). So it creates a (non-temporary) table, based on the supplied data frame. This table obviously persists across connections. is a different capability), then dbWriteTable fails here.

What is the use of dbexiststable in dbwritetable?

Quick semantics - dbWriteTable uses dbExistsTable as a test internally when overwrite=TRUE to see whether it should drop the existing table. The problem you are running into comes from dbExistsTable and dbWriteTable understanding schemas differently. library ( odbc ) library ( DBI ) con <- dbConnect ( odbc:: odbc (), ...


1 Answers

This solution was listed by the author in the question and has been moved here.

It appears the bug/feature only occurs when a full table path is used such as myDB.temp_table compared to simply temp_table

> dbWriteTable(conn=open_connection, name='myDB.temp_table', value=summary_data_final, overwrite=FALSE, append=TRUE, row.names=0)
Error in mysqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: Table 'temp_table' already exists)
[1] FALSE
Warning message:
In mysqlWriteTable(conn, name, value, ...) :
  could not create table: aborting mysqlWriteTable

> dbWriteTable(conn=open_connection, name='temp_table', value=summary_data_final, overwrite=FALSE, append=TRUE, row.names=0)
[1] TRUE

I haven't found this documented anywhere and am surprised that I haven't come across it before.

like image 161
Shawn Avatar answered Sep 20 '22 16:09

Shawn