删除sqlite数据库文件

发布于 2024-11-15 19:07:36 字数 732 浏览 1 评论 0原文

我正在开发一个 C# 应用程序,其后端为 sqlite。在我的应用程序中,我有一个用于清理数据库的按钮(删除 .db 文件并使用一些初始数据再次创建它)。有时,当我尝试删除数据库时,它说无法删除它,因为它正在被另一个进程使用。在删除它之前,我使用关闭连接、处置和清除池功能。即使这样它也会抛出相同的异常。这是我的代码:

string targetDataBaseFilePath = Path.Combine(dataDirectoryPath, "local.db");               
ThingzDatabase.Create(targetDataBaseFilePath, "touchdb");

在创建函数中,我定义了以下代码,从那里我得到了错误:

if (File.Exists(DatabaseFileName))
{
    try
    {
        ThingzDatabase.localdb.conn.Close();
        ThingzDatabase.localdb.conn.Dispose();
        SQLiteConnection.ClearAllPools();
    }
    catch (Exception)
    {
    }
    File.Delete(DatabaseFileName); //error throws from here.                       
}

如何防止抛出错误?

I am developing a C# application with its backend as sqlite. In my application I have a button for cleaning the database (deleting the .db file and creating it again with some initial data). Sometimes when I try to delete the db it says it cannot be deleted because it is being used by another process. Before deleting it I am using close connection, dispose and clear pool functions. Even then it throws the same exception. Here is my code:

string targetDataBaseFilePath = Path.Combine(dataDirectoryPath, "local.db");               
ThingzDatabase.Create(targetDataBaseFilePath, "touchdb");

In the create function I define the following code, from where I get the error:

if (File.Exists(DatabaseFileName))
{
    try
    {
        ThingzDatabase.localdb.conn.Close();
        ThingzDatabase.localdb.conn.Dispose();
        SQLiteConnection.ClearAllPools();
    }
    catch (Exception)
    {
    }
    File.Delete(DatabaseFileName); //error throws from here.                       
}

How can I prevent the error from being thrown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

层林尽染 2024-11-22 19:07:36
FileInfo fi = new FileInfo(DatabasePath);
try
{
    if (fi.Exists)
    {
        SQLiteConnection connection = new SQLiteConnection("Data Source=" + DatabasePath + ";");
        connection.Close();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        fi.Delete();
    }
}
catch(Exception ex)
{
    fi.Delete();
}

DatabasePath 是您的数据库文件路径

FileInfo fi = new FileInfo(DatabasePath);
try
{
    if (fi.Exists)
    {
        SQLiteConnection connection = new SQLiteConnection("Data Source=" + DatabasePath + ";");
        connection.Close();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        fi.Delete();
    }
}
catch(Exception ex)
{
    fi.Delete();
}

DatabasePath is your database file path

一世旳自豪 2024-11-22 19:07:36
ThingzDatabase.localdb.conn.ClearAllPools(); 

我相信应该是对该行的调用。

ThingzDatabase.localdb.conn.ClearAllPools(); 

should be the call for that line I believe.

栩栩如生 2024-11-22 19:07:36

我最终只是自己问了问题。解决方案是让垃圾收集运行一段时间。

I ended up just asking the question myself. The solution was to let garbage collection run for a bit.

千柳 2024-11-22 19:07:36

我相信你还没有处理从 API 返回的东西。

你使用哪个 sqlite 包装器?您引用了自定义函数的使用,它们是做什么的?

I beleive you still haven't disposed something returned from API.

Which sqlite wrapper you use? You are quoting use of your custom functions, what do they do?

岁月静好 2024-11-22 19:07:36

在连接字符串中使用此 Pooling=False;我在池化时遇到了同样的问题。

In connection string use this Pooling=False; I had the same problem with pooling on.

烂柯人 2024-11-22 19:07:36

您可以使用 Mark Russinovich 的 one 这样的 procmon。
然后您可以根据文件名过滤显示。
完成此操作后,您可以准确地看到哪个进程正在锁定该文件,并进行相应的调试。

You can use a procmon like this one by Mark Russinovich.
Then you can filter the display on the basis of the filename.
Once this is done, you can see exactly which process is locking this file, and debug accordingly.

很糊涂小朋友 2024-11-22 19:07:36

我看到很多与流程相关的工作。但 sqlite 有一个方法。
真空

VACUUM 命令重建数据库文件,将其重新打包到最小的磁盘空间中。

https://sqlite.org/lang_vacuum.html

I see a lot working with processes. But sqlite has a method for it.
VACUUM

The VACUUM command rebuilds the database file, repacking it into a minimal amount of disk space.

https://sqlite.org/lang_vacuum.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文