6
I would like to "merge" two tables of a Sqlite "database" from the R -- and save it to a new table within the same database. Below, I send a minimum reproducible code:
install.packages("sqldf",dependencies=T)
install.packages("RSQLite",dependencies=T)
library(sqldf)
A <- data.frame(var1 = 1:5, var2=55:59)
B <- data.frame(var1 = 11:15, var2=155:159)
drv <- dbDriver("SQLite")
con <- dbConnect(drv, "basequalquer.db")
dbWriteTable(con, "TabelaA", A)
dbWriteTable(con, "TabelaB", B)
The result I desire is this one:
dbGetQuery(con, 'SELECT * FROM TabelaA UNION ALL SELECT * FROM TabelaB' )
row_names var1 var2
1 1 1 55
2 2 2 56
3 3 3 57
4 4 4 58
5 5 5 59
6 1 11 155
7 2 12 156
8 3 13 157
9 4 14 158
10 5 15 159
I can do a query to get it, but I don’t know how to save it directly in the database (that is, without having to store it in a data.frame and then do dbWriteTable)
I think when you install sqldf Rsqlite comes as a gift as dependency. The sqldf cat jump is that you do not need to use dbWriteTable, when there is a data.frame X and you use sqldf("SELECT * FROM X") df X is automatically copied to Sqlite.
– Lucas Soares