Search code examples
rpathrsqlite

Connect to SQLite db without changing wd


Is there a way to specify a connection path to an SQLite database from R without re-setting my wd?

I've looked through the DBI::dbConnect() and RSQLite::SQLite() documentation, but it is still not clear to me how (or if) this can be accomplished. So far, the only solution I've come across is to setwd(), but this is something I'd like to avoid if possible.

Instead I'd rather include a path, something like...

library(DBI)
library(RSQLite)

path_db <- "E:/"

# ... this still looks in my current wd... how to I get it to look on the E-drive?
con <- dbConnect(SQLite(), "dummy_data.db")

... but I can't figure out how I should get the path in there... most of the tutorials {1, 2} I've come across seem to assume the database is in the working directory.


Solution

  • You just need to specify the path in the db name:

    library(DBI)
    library(RSQLite)
    
    path_db <- "E:"
    
    con <- dbConnect(SQLite(), file.path(path_db,'test.db'))
    

    make sure that you have write access to E: to avoid :

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