-1

When trying to connect to this database by copying their code I get the error:

"Could not connect to database: unable to open database file"

The code :

tidy_finance <- dbConnect(
  SQLite(),
  "data/tidy_finance.sqlite",
  extended_types = TRUE
)
Phil
  • 7,287
  • 3
  • 36
  • 66
  • How have you organized your code - are you using R project or it's a stand-alone R / Rmd / .. file? Where, in relation to your project or source file, is the `data/` folder? Have you checked your working directory `getwd()` and is it the same path where your `data/` is, i.e. do you have `"data"` in `list.files()`? – margusl Oct 13 '22 at 16:46
  • Thanks. So I thought the "data/tidy_finance.sqlite" which they provide in their code, is coming from their database. As I don't have any folder "data" in my working directory with the a file "tidy_finance.sqlite". But I should then? Im using R-Markdown. https://www.tidy-finance.org/accessing-managing-financial-data.html this is the code im replicating. – bernard202210 Oct 14 '22 at 07:15
  • Actually you should be all set as soon as you create empty 'data' folder (or remove that part from sqlite file path). It should create new empty sqlite db file, but starts to complain if it can't find destination folder. – margusl Oct 14 '22 at 09:03

1 Answers1

0

Collecting comments to an answer.

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

Either opens the existing data/tidy_finance.sqlite file or if the file is missing, creates an empty database. Problem arises when it can't find existing data/ directory. One could just remove directory from sqlite file path, but we can also check and create the missing folder in R before attempting to create / open sqlite file.:

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"

Created on 2022-10-14 with reprex v2.0.2

margusl
  • 7,804
  • 2
  • 16
  • 20