1

I have created an empty table in a mysql database with the following specification

    Field          Type Null Key           Default                       Extra
1      Id       int(11)   NO PRI              <NA>              auto_increment
2    Date     timestamp   NO     CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
3 Country   varchar(45)  YES                  <NA>                            
4   Brand   varchar(45)  YES                  <NA>                            
5   Value decimal(10,0)  YES                  <NA>                          

Now I would like to use sqlSave from RODBC package to append data in a data frame to this table. My data frame looks like this

     Id       Date Country  Brand Value
591 591 2013-06-22      DK bet365    44
603 603 2013-09-14      DK bet365    69
362 362 2009-01-31      DK bet365    22
296 296 2013-08-31      DK unibet    33
216 216 2012-02-18      DK unibet    24
261 261 2012-12-29      DK unibet    28
326 326 2008-05-24      DK bet365    18
521 521 2012-02-18      DK bet365    71
494 494 2011-08-13      DK bet365    44
558 558 2012-11-03      DK bet365    68

and can be recreated by

mydf<-structure(list(Id = c(591L, 603L, 362L, 296L, 216L, 261L, 326L, 521L, 494L, 558L), 
                 Date = c("2013-06-22", "2013-09-14", "2009-01-31", "2013-08-31", "2012-02-18", "2012-12-29", "2008-05-24", "2012-02-18", "2011-08-13", "2012-11-03"), Country = c("DK", "DK", "DK", "DK", "DK", "DK", "DK", "DK", "DK", "DK"), 
                 Brand = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
                                   .Label = c("unibet", "bet365", "betsafe"), 
                                   class = "factor"), 
                 Value = c(44, 69, 22, 33, 24, 28, 18, 71, 44, 68)), 
            .Names = c("Id", "Date", "Country", "Brand", "Value"), 
            row.names = c(591L, 603L, 362L, 296L, 216L, 261L, 326L, 521L, 494L, 558L), 
            class = "data.frame")

I then run the following command

sqlSave(channel, dat=mydf, tablename="GoogleTrends", rownames=FALSE, append=TRUE)

which results in an error:

Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test,  : 
  missing columns in »data«

I've tried adding varTypes but still no luck. I've also tried with sqlUpdate but cannot get that to work either. To be clear, I can easily make this work if I allow sqlSave to create the table itself. So I guess there's some convertion problem but I cannot detect it. Anyone knows how to solve this?

Dr. Mike
  • 2,451
  • 4
  • 24
  • 36

3 Answers3

0

could I ask how you defined varTypes? I thought using varTypes would work. What I would do is to retrieve the variable type from the table already created, and get R to map to it. Within R you can get the vartypes of the table already created by using something like this tmp <- sqlColumns(channel, tablename) and what you want is varspec <- tmp$TYPE_NAME.

0

I got the same problem on my Windows System using MySQL ODBC driver version 5.2. The case="nochange" argument in odbcConnect did the trick. Without this argument sqlSave lowercases the column names in the data frame.

André le Blond
  • 368
  • 3
  • 8
0

I ran into a very similar issue and found from here Problems with RODBC sqlSave that turning fast=F resolves the problem.

rookieJoe
  • 509
  • 6
  • 14