Search code examples
rsqlite

RSQLite does not create local database


Reprex:

con <- DBI::dbConnect(RSQLite::SQLite(), path = "test.sqlite")
dbWriteTable(con, "mtcars", mtcars)
dbListTables(con)
[1] "mtcars"
dbDisconnect(con)

When I come back:

con <- DBI::dbConnect(RSQLite::SQLite(), path = "test.sqlite")
dbListTables(con)
character(0)

I thought dbConnect should create a database if none exists. I don't know what is going on.


Solution

  • To create a local database you still need to supply parameters specific to your machine. This fixed the issue for me:

    con <- DBI::dbConnect(RSQLite::SQLite(),
                      user = 'root',
                      password = '',
                      dbname = 'test.sqlite',
                      host = 'localhost')
    

    This is documented poorly if you do not have much understanding of SQLite databases. It seems a database was only being created in memory. Maybe someone else can expound on this as I think warnings would help guide users in this situation.