How do you explicitly delete a SQLite database created with the sqldf library in an R script

故事扮演 提交于 2019-12-25 05:59:29

问题


I have created an R function to perform subsetting, summaries, densities, and plotting. I was initially assigning out the subsets to my workspace in RStudio but I started running into memory constraints. The latest revision is attempting to store the summarized observation counts in a SQLite database vs. exporting the subsets as their own dataframes. The theory was that this would utilize less memory. In order to perform this process I created a new database in my function as in:

sqldf("attach 'mydb' as new")
sqldf("create table TotalsTbl as select 'resimrr' as name, count(*) as count from resimrr", dbname="mydb")
sqldf("insert into TotalsTbl select 'resimrrquan' as name, count(*) as count from resimrrquan", dbname="mydb")

I then create a few tables, and populate them as the function processes. Finally I export the query results of SQLite Tables to dataframes that I then assign out to my workspace.

This provides the expected result:

sqldf("select * from TotalsTbl, dbname="mydb")
name count
1 resimrr 95517
2 resimrrquan 93928

The challenge now is that the database is persistent. It is created the first time the function is envoked, it lives in my Rsessions and my workspace and if I run the function again the CREATE TABLE commands fail because the tables already exist. So the question is how do I delete/clean up 'mydb' after I am through with it in my function?

来源:https://stackoverflow.com/questions/16593364/how-do-you-explicitly-delete-a-sqlite-database-created-with-the-sqldf-library-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!