I am using an SQLite database in R. To insert a data frame into an SQLite table using dbWriteTable from DBI, it seems that I need to have the same column names in the data frame as those in the table. I was using sqldf and this was not a condition, it just insert based on the order of columns. Is there a way to change the behaviour of dbWriteTable to accept my data frame. Here is an example code with both dbWriteTable and sqldf
library(RSQLite)
library(sqldf)
path = "data_base.sqlite"
conn = DBI::dbConnect(RSQLite::SQLite(),path)
dbExecute(conn, "CREATE TABLE sales(Items INT, Sales REAL)")
df1 = data.frame(Items = c(12,14,5), Sales = c(111.6,130.2,46.5))
dbWriteTable(conn,name = "sales",value = df1, append=TRUE, row.names=FALSE)
df2 = data.frame(Nombre = c(2,6,9), Ventes = c(18.6,55.8,83.7))
dbWriteTable(conn,name = "sales",value = df2, append=TRUE, row.names=FALSE)
sqldf("insert into sales select * from `df2`",dbname = path)