I am trying to insert data to sqlite database from R data.frame but I failed.
Here is the code in R studio I used.
So there two loops using which I'm trying to load all dbf files listed in the specified folders (working directory). And then I'm trying to insert data from R data.frame (df[[1]]) to the sqlite database (I have already created it) by sqldf or by dbExecute functions.
In case of dbExecute the function cannot read table from R data.frame (in this case df[[1]]).
In case of sqldf the function doesn't see all_banks table created earlier in the database.
Any ideas of how to handle this problem? Thank to all.
library(sqldf)
library(DBI)
library(foreign)
library("RSQLite")
setwd("F:~/Data")
con <- dbConnect(RSQLite::SQLite(), dbname = "banks.db")
for(path in c("F:~/123-20190901",
"F:~/123-20190801")){
setwd(path)
ldf <- list()
listdbf <- dir(pattern = "*.DBF")
for (k in 1:length(listdbf)){
ldf[[k]] <- read.dbf(listdbf[k])
}
df1 <- ldf[[1]]
df2 <- ldf[[2]]
dbExecute(con, "insert into all_banks select DT, REGN, name_b from df1")
sqldf("insert into all_banks select DT, REGN, name_b from df1")
}
dbDisconnect(con)
Error: no such table: df1
Error: no such table: all_banks