0

I have a problem with RODBC;this is the error :

 chargerExp("C:\\test.csv",NE=1,NC=1,s=1)
 Exeperience: 1    Execution: 1    Sujet: 1 

> ajouter(new ("BDD"),new("Exp"))
[1] "42000 1064 [MySQL][ODBC 5.2(a) Driver][mysqld-5.6.17]You have an error in your SQL syntax; 
check the    manual that corresponds to your MySQL server version for the right syntax to use near '  ,  '     NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/' at line 1"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`, 
`Z`,  `T`, `A`, `S`) VALUES (  ,  ,  ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ '     , ' NA/NA/NA/ ' ,  ,   );'"                                               

I'm using Mysql, RODBC and Rstudio with a method ajouter to insert in the data base

setMethod( f ="ajouter",signature =c(x="BDD",obj="Exp"),
       def = function(x, obj)
       {

         channel <- odbcConnect(dsn="RSQL",uid="root",pwd="toor")
         ne <- obj["ne"]
         nc <- obj["nc"]
         s <- obj["S"]
         e<- encoder((obj["E"]))
         x <- encoder((obj["X"]))
         y <- encoder((obj["Y"]))
         z <- encoder((obj["Z"]))
         t<- encoder((obj["T"]))
         a <- (obj["A"])
         requeteSql.valeur <- paste("'",e,"'",",",
                                    "'",x,"'",",",
                                    "'",y,"'",",",
                                    "'",z,"'",",",
                                    "'",t,"'",",")

         requetesql <- paste("INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`, `Z`, `T`, 
        `A`, `S`) VALUES (",ne,",",nc,", ",requeteSql.valeur, a,", ",s,");")
         sqlQuery(channel, requetesql)
       }       
)

This is encoder method. It's main purpose is to convert my object to a text with some concatenation.

setMethod( f ="encoder", signature ="Para",
       def =function(x, i, j, value)
       {
         s <- as.character(x["val"][1])
         for(i in 2:length(x["val"]))
         {
           s <- paste(s,x["val"][i],sep="/")
         }
         return(s)
       }
)

The call of my methods :

ajouter(new ("BDD"), new("Exp"))

This is my table in data base :

          CREATE TABLE `test` (
        `idTest` int(11) NOT NULL AUTO_INCREMENT,
         `NE` int(11) DEFAULT NULL,
         `NU` int(11) DEFAULT NULL,
         `E` text,
         `X` text,
         `Y` text,
         `Z` text,
          T` text,
          `A` float DEFAULT NULL,
          `S` int(11) DEFAULT NULL,
           PRIMARY KEY (`idTest`)
        ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

My problem is from sqlquery, it said that my syntax are not correct, I don't know why.

This is the the object I want to load into the database (from dput so copy/pasteable):

     new("Para"
      , ne= 1
      , nc = 1
      , E = new("E"
      , val = c(-13, -11, -11, -11, -11, -9, -10, -12, -12, -12, -11, -10, 
      -8, -8, -8, -9, -9, -7, -9, -10, -9, -9, -9, -9, -11, -9, -7, 
      -7, -7, -7, -7, -8, -7, -7, -5, -5, -7, -8, -7, -5, -5, -6, -6, 
      -9, -35, -76, -96, -62, 38, 167, 251, 251, 248, 157, 94, 56, 
      )
      )
    , X = new("Para"
    , val = c(115, 116, 114, 113, 113, 114, 115, 114, 114, 113, 112, 111, 
      113, 114, 114, 115, 115, 116, 115, 115, 114, 116, 114, 115, 114, 
      113, 114, 114, 114, 114, 114, 113, 113, 114, 114, 114, 114, 115, 
      114, 115, 115, 115, 114, 115, 116, 114, 114, 114, 116, 115, 113, 
      )
      )
     , Y = new("Para"
    , val = c(10, 11, 9, 9, 10, 9, 10, 9, 11, 10, 11, 10, 11, 11, 10, 11, 
      10, 10, 11, 10, 9, 10, 11, 12, 11, 10, 11, 11, 11, 12, 11, 11, 
      11, 10, 11, 11, 10, 12, 10, 11, 11, 11, 11, 11, 11, 10, 11, 11, 
      10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 10, 12, 11, 10, 10, 10, 
      )
      )
    , Z = new("Para"
    , val = c(-42, -42, -44, -43, -42, -41, -42, -42, -42, -42, -42, -43, 
      -40, -41, -41, -40, -41, -43, -41, -41, -41, -41, -41, -40, -40, 
      -41, -40, -40, -41, -40, -42, -41, -41, -41, -41, -41, -43, -42, 
      -43, -42, -41, -42, -42, -40, -41, -42, -40, -41, -41, -41, -42, 
      )
      , T = new("Para"
     , val = c(25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 
       25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 
      25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 
      25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 
      )
      )
   , A = 1L
   , S  = 1
    )
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
foboss
  • 430
  • 4
  • 14
  • This isn't reproducible at all without some data. Can you create a *small* example that illustrates your problem? See [here](http://stackoverflow.com/q/5963269/903061) for suggestions. – Gregor Thomas May 20 '14 at 18:40
  • a) Please edit code into your question instead of leaving it in a comment. b) You have two functions that do two very separate pieces of work: reading a .CSV file into R and trying to write an R object to a database. Almost certainly, you have a problem with one of the pieces but not both. Can you test them individually and simplify your problem by only providing info related to the broken piece? – Gregor Thomas May 20 '14 at 18:58
  • @shujaa The problem is in the second function (ajouter) ,when i try to write into database it gives me errors about my query .I just put an example of the csv file and my table,i use the encode function to turn my values into string for storage (because they are numeric ,see csv file). – foboss May 20 '14 at 19:07
  • So, `chargerExp` doesn't matter, you can remove it and the CSV from your question and just give us `dput(ObjectCreatedByCharger)`. – Gregor Thomas May 20 '14 at 19:13
  • Otherwise anyone who wants to help you has to copy/paste your CSV into a file and save, run `chargerExp` to see what you get, just to get to a starting place. – Gregor Thomas May 20 '14 at 19:17
  • @shujaa chargerExp is like opencsv in java ,the output is my csv file parsed – foboss May 20 '14 at 19:21
  • @shujaa this is my output Exeperience: 1 Execution: 1 Sujet: 1 – foboss May 20 '14 at 19:22
  • Please use `dput` so that the structure of your data is clear and it's easy to copy-paste. – Gregor Thomas May 20 '14 at 19:27
  • ... and edit it into your question, the whole thing, rather than leaving it in a comment. I'm trying to get your question into a reasonable shape. Perhaps take a look at the link in my first comment. – Gregor Thomas May 20 '14 at 19:30
  • @shujaa Sorry but my data are too big ,let me 2 min please – foboss May 20 '14 at 19:33
  • @shujaa I put the output of the function ,the parsing is ok!!! – foboss May 20 '14 at 19:55
  • I still can't make sense of what's going on... you use so many classes I don't have definitions for that I can't run anything on my computer. Try putting a `browser()` call in you `ajouter` definition after the `requetesql` line and inspect how things are. – Gregor Thomas May 20 '14 at 20:48
  • @shujaa I'am sorry my computer bug and turnoff many times when i'am doing this ,if you don't mind i will give u my full program;u can find it here (https://www.dropbox.com/s/kkgjj8mygqels0x/ds.R) – foboss May 20 '14 at 21:25

1 Answers1

0

I downloaded your code and there seem to be many problems, the lest of which is actually running the query you tried. The error tells you it tried to run the SQL command

INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`, `Z`,  `T`, `A`, `S`)
VALUES (  ,  ,  ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' ,
     ' NA/NA/NA/ '     , 'NA/NA/NA/ ' ,  ,   );'

And it should be clear that looks invalid. I don't think you can have emply values in an INSERT statement like that.

The blank values are from the lines

ne <- obj["ne"]
nc <- obj["nc"]
s <- obj["S"]

In your function definition, obj is your Exp (or Experimentation) and you simply haven't set any values for these slots yet. Since they are empty vectors, they do not render as anything in the paste statement.

The batman values (aka NA/NA/NA) are from the

e<- encoder((obj["E"]))
x <- encoder((obj["X"]))
y <- encoder((obj["Y"]))

lines. When the Signal class is empty (as it is when you create a new, empty object) the val slot is just an empty numeric vector. So when you go to encode it, you've set up a loop that's basically going

s <- NA
for(i in 2:1)
{
    s <- paste(s,NA,sep="/")
}

which is resulting in that NA/NA/NA/ value. And really that loop in unnecessary, a simple

s <- paste(as.character(x["val"]), collapse="/")

would give you the string you want without the counting-backwards problem.

So really, running ajouter(new ("BDD"),new("Exp")) with empty objects like that doesn't make much sense. You really should test each of your classes first by themselves before putting them together like that. I can't believe you didn't get other errors or warnings before that. You should work on isolating your errors and testing each part individually.

MrFlick
  • 195,160
  • 17
  • 277
  • 295