由于这里不解释的原因,我需要使用同一个连接对象来读取 dbplyr 中的两个数据库。我找到了一些在线资源,但我没有做对。请看下面的reprex。谁能告诉我我做错了什么?非常感谢!
library(tidyverse)
library(DBI) # main DB interface
library(dbplyr) # dplyr back-end for DBs
#>
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#>
#> ident, sql
library(RSQLite)
##create the databases
df1 <- tibble(x=1:20,y=rep(c("a", "b"), 10))
df2 <- tibble(x=101:120,y=rep(c("d", "e"), 10))
con <- dbConnect(drv=RSQLite::SQLite(), dbname="db1.sqlite")
dbWriteTable(con,"mydata1",df1, overwrite=T)
dbDisconnect(con) # closes our DB connection
con <- dbConnect(drv=RSQLite::SQLite(), dbname="db2.sqlite")
dbWriteTable(con,"mydata2",df2, overwrite=T)
dbDisconnect(con) # closes our DB connection
## Now that I have created the two databases, I try reading them with the same connection object
con <- dbConnect(drv=RSQLite::SQLite())
db1 <- tbl(con, in_schema("db1.sqlite","mydata1"))
#> Error: no such table: db1.sqlite.mydata1
db2 <- tbl(con, in_schema("db2.sqlite","mydata2"))
#> Error: no such table: db2.sqlite.mydata2
### but this fails miserably. How to fix it?
由reprex 包于 2020-12-24 创建(v0.3.0)