I'm building a package for internal usage and attempting to abstract away all possible database interaction from the users. I need to connect to the database and disconnect from the database within the function (I think). However, the disconnect does not work.
`my_func = function(){
con = DBI::dbConnect(RSQLite::SQLite(), 'db_location.sqlite')
r = DBI::dbSendQuery("SELECT * ...")
dat = DBI::dbFetch(r)
DBI::dbDisconnect(con)
return(dat)
}`
If you call the function:
MY_LIBRARY::my_func()
Data is returned but the connection does not terminate and a warning is displayed.
`Warning message:
In connection_release(conn@ptr) :
There are 1 result in use. The connection will be released when
they are closed`
dbClearResult(r)
? You might preferDBI::dbGetQuery
instead ofdbSendQuery
/dbFetch
, as it clears the result automatically. – Pillage