sqlite 无法连接到SQLlite数据库

g6ll5ycj  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(354)

当尝试通过复制their code连接到此数据库时,我收到错误:
“无法连接到数据库:无法打开数据库文件”
代码:

tidy_finance <- dbConnect(
  SQLite(),
  "data/tidy_finance.sqlite",
  extended_types = TRUE
)
gywdnpxw

gywdnpxw1#

收集对答案的评论。

dbConnect(SQLite(),"data/tidy_finance.sqlite",extended_types = TRUE)

打开现有的data/tidy_finance.sqlite文件,或者如果该文件丢失,则创建一个空数据库。当它找不到现有的data/目录时就会出现问题。用户只需从SQLite文件路径中删除目录,但我们也可以在尝试创建/打开SQLite文件之前检查并创建R中缺少的文件夹。

library(RSQLite)
library(dbplyr)

list.files(include.dirs = T, recursive = T)
#> [1] "sqlite_test.R"
# no data/ in working directory
# dbConnect() will fail:
tidy_finance <- dbConnect(SQLite(), "data/tidy_finance.sqlite",extended_types = TRUE)
#> Error: Could not connect to database:
#> unable to open database file

# create the directory if missing, open and close db connection, check file listing
if (!dir.exists("data/")){
  dir.create("data/")
}
tidy_finance <- dbConnect(SQLite(), "data/tidy_finance.sqlite",extended_types = TRUE)
dbDisconnect(tidy_finance)
list.files(include.dirs = T, recursive = T)
#> [1] "data"                     "data/tidy_finance.sqlite"
#> [3] "sqlite_test.R"

创建于2022-10-14,reprex v2.0.2

相关问题