1

I need to upload data from R into Oracle database. I follow to the recommendations from this question how to export data frame (R) into Oracle table.

I use the code:

dbWriteTable(con_dwh, "table_db", table_in_R, 
         append =T, row.names=F, overwrite = F)

where con_dwh is a JDBC conection to Oracle database.

Hovewer I've got the error message:

Error in .local(conn, statement, ...) : 
execute JDBC update query failed in dbSendUpdate (NaN)

I am a little bit confused on the error occured.

Could someone help me?

6
  • It might be that one of the rows has a value that is not interpreted correctly. Try inserting one row. If it works, add 10 or 50 rows (depending on how much you have). Keep going until you hit the failure again, then try to identify which row of the new batch is the culprit. If finding the row doesn't make it abundantly clear which value is at fault, come back and edit your question to include at least one good row and one bad row. Good luck! Commented Jan 14, 2022 at 13:33
  • @r2evans I've tried to insert 1 row. Result - error ORA-00947. I have no idea why this error occurs. Could this be due to the identity column values of which myst be generated automatically when inserting data? Commented Jan 14, 2022 at 13:38
  • I don't work with Oracle, so I can really only help advise the process to determine the problem. Perhaps in addition to the "individual rows" thing I suggested, you also try single or small sets of columns until you can insert without error, and then keep working up to resolve which column is doing it. (BTW: ORA-00947 suggests empty values, either "" or NA, might be an issue. I also don't use JDBC, preferring the non-java odbc, so perhaps that is also at play here. Commented Jan 14, 2022 at 14:01
  • 1
    ORA-00947 simple means that the table has more columns that the data frame. Simple use overwrite = T, append = F and it disappears... Commented Jan 14, 2022 at 15:40
  • @MarmiteBomber indeed the problem disappeared. But I faced another problem that R couldn't insert rows with na. How to hadle it? Commented Jan 14, 2022 at 19:12

1 Answer 1

1

For some reasons in dbWriteTable a NA value in a num columns fails to be inserted as NULL, which works fine for chr column.

So the workaround is to replace the NA in the num column with a valid number - i#m using `0``.

If you need to end with nullin the table use some special number and UPDATE to null in the database.

> df <- data.frame(col1 = c('x',NA,'y'), col2 = c(1,NA,NA))
> 
> df
  col1 col2
1    x    1
2 <NA>   NA
3    y   NA
> str(df)
'data.frame':   3 obs. of  2 variables:
 $ col1: chr  "x" NA "y"
 $ col2: num  1 NA NA
> 
> dbWriteTable(jdbcConnection,"TEST", df,   rownames=FALSE, overwrite = TRUE, append = FALSE)  
Fehler in .local(conn, statement, ...) : 
  execute JDBC update query failed in dbSendUpdate (NaN)
> 
> df[is.na(df$col2),"col2"] <- 0
> df
  col1 col2
1    x    1
2 <NA>    0
3    y    0
> 
> dbWriteTable(jdbcConnection,"TEST", df,   rownames=FALSE, overwrite = TRUE, append = FALSE) 
> 

In the DB

select * from test

COL1           COL2
-------- ----------
x                 1
                  0
y                 0

For integer data type int the insert of NA works but the inserted value is -2147483648 which is also not good.

df <- data.frame(  id  = c(as.integer(NA),as.integer(NA)) )
dbWriteTable(jdbcConnection,"TEST", df,   rownames=FALSE, overwrite = TRUE, append = FALSE) 

## OK but

select * from test

ID
-2147483648
-2147483648

This has probably to do with the internal storage of NA as integer.

Note produced with R version 4.0.4 and RJDBC_0.2-8.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.