Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using RODBC to insert data frames to oracle table. I have Primary key set up on the table and if a duplicate data frame comes in it will oracle will reject it. In order to make sure I am not missing any data, first I try to insert the whole data frame, if any errors, I will try to insert each record at a time. But when I try to insert each record at a time, it is taking a very long time to finish. I was curious, has anybody done any type of work like this and what was the approach. My code is below:

tryCatch({

        ch=odbcConnect("<id>",pwd = "<password>")
        sqlSave(ch,dat, tablename="<tablename>", rownames=FALSE, append=TRUE)
        },error = function(e) 
        {
          print("unable to insert the data frame, will try by each row")   
                                    ch=odbcConnect("<id>",pwd = "<password>")

             for (k in 1:nrow(dat)) 
                {
                    j<-dat[k,]
                    tryCatch({
                            sqlSave(ch,j, tablename="<tablename>", rownames=FALSE, append=TRUE)
                    },error = function(e) {
                        print("unable to insert, duplicate values")
                    })
                }
                odbcClose(ch)
        })
odbcClose(ch)
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Move the odbcConnect out of the per-record loop.

share|improve this answer
    
thank you for the tip. I did that but still takes a very long time. I modified the original post. –  user1471980 Apr 29 '13 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.