Write data into oracle database in r using loop -


i able make market basket analysis in r on database. i've finished analysis , want write results oracle database row row. i've tried this

sonuclar<-inspect(basket_rules[1:5]) mode(sonuclar) [1] "list" class(sonuclar) [1] "data.frame" for(row in 1:nrow(sonuclar)) {`dbgetquery(jdbcconnection,paste0("insert market_basket_analysis (lhs,rhs,support,confidence,lift) values ('",sonuclar$lhs[row],"','",sonuclar$rhs[row],"','",sonuclar$support[row],"','",sonuclar$confidence[row],"','",sonuclar$lift[row],"')"))}` 

but writes 1 row , doesn't work other loop iteration steps , returns error message :

`error in .verify.jdbc.result(md, "unable retrieve jdbc result set meta data ",  :    unable retrieve jdbc result set meta data insert market_basket_analysis (lhs,rhs,support,confidence,lift) values ('{sprite gazoz1,5l}','{cocacola # 1,5lt}','0.00395004588437138','0.439024390243902','1.99010097534508') in dbsendquery (ora-00900: invalid sql statement )` 

even though gives error , inserted

{sprite gazoz1,5l},{cocacola # 1,5lt},'0.00395043993535644','0.439024390243902','1.98990246549237'  

this first row.

i have not found meaningful information in internet although have searched lot on week ; have idea solution error. in advance.

for me bit surprising did not work, yes, easy reproduce code provide. until moment, whenever had similar i've used dbwritetable, wonder if can't whatever reason.

anyway, if need or want insert in loop, should use bind variables. loop this,

for(row in 1:nrow(sonuclar)) {  inss <-  paste0("insert temp (lhs,rhs,support,confidence,lift) values (?, ?, ?, ?, ?)")  dbsendupdate(con, inss, list=as.list(sonuclar[row, ])) } 

(note connection called con , table temp, instead of names...) reference, i've defined data.frame this,

sonuclar <- data.frame(matrix(c('{sprite gazoz1,5l}','{cocacola # 1,5lt}',0.00395004588437138,0.439024390243902,1.99010097534508,                                 '{sprite gazoz1,5l}','{cocacola # 1,5lt}',0.00395043993535644,0.439024390243902,1.98990246549237),                               nrow=2, ncol=5, byrow=true)) colnames(sonuclar) <- c('lhs','rhs','support','confidence','lift') 

and in table i've used varchar(20) lhs , rhs, , number(10,4) other columns.

a final note, don't know how big loop, better if disabled autocommit (by default true),

.jcall(con@jc, "z", "getautocommit") [1] true .jcall(con@jc, "v", "setautocommit", false) 

but need commit outside of loop, just

dbcommit(con) 

hope helps.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -