如何使用 Python 3 压缩和修复 Access 数据库

Posted

技术标签:

【中文标题】如何使用 Python 3 压缩和修复 Access 数据库【英文标题】:How to compact and repair an Access database using Python 3 【发布时间】:2019-07-26 09:31:05 【问题描述】:

我正在尝试使用 Python 3(在我的情况下特别是 3.7)在 Windows 10 上为 Access 2016 数据库运行“压缩和修复数据库”功能。

有一个示例 here,但这似乎适用于 Python 2.7 和 MS Access 2010。我已将语法稍微调整为:

import os
import win32com.client

srcDB = r'C:\My\Database\Path\My_Db.accdb'
destDB = r'C:\My\Database\Path\My_Db_PyBackup.accdb'

oApp = win32com.client.Dispatch("Access.Application")
oApp.compactRepair(srcDB, destDB)
oApp.Application.Quit()
oApp = None

os.remove(destDB)

我没有收到任何错误,并且脚本成功运行。但是,文件大小保持不变 - 我仍然需要打开数据库并手动“压缩和修复数据库”。

因此,我想知道是否有其他方法适用于较新版本的 Python 和 Access,或者我是否遗漏了什么。

【问题讨论】:

你压缩到destDB 然后删除它。用新压缩的destDB 替换srcDB 文件的命令在哪里? @4dmonster 那么,destDB 是压缩数据库吗?我明白这只是一个备份。在这种情况下,我可以改为删除原始文件,即 srcDB 并重命名 destDB 有人在链接问题中提到oApp = win32com.client.Dispatch("Access.Application")应该是oApp = win32com.Dispatch("Access.Application") @Frieder 我也注意到了这一点。但是,如果我尝试使用该语法,我会收到 AttributeError: module 'win32com' has no attribute 'Dispatch' 消息 CompactRepair(SourceFile, DestinationFile, LogFile)SourceFile Required String - The full path and filename of the database or project file to compact and repair. DestinationFile Required String - The full path and filename for where the recovered file will be saved. LogFile Optional Boolean - True if a log file is created in the destination directory to record any corruption detected in the source file. A log file is only created if corruption is detected in the source file. If LogFile is False or omitted, no log file is created, even if corruption is detected in the source file. 【参考方案1】:

感谢4dmonster对compactRepair()函数的更详细的解释,看来上面确实按预期工作了:

CompactRepair(SourceFile, DestinationFile, LogFile) 拥有SourceFile Required String - The full path and filename of the database or project file to compact and repair. DestinationFile Required String - The full path and filename for where the recovered file will be saved. LogFile Optional Boolean - True if a log file is created in the destination directory to record any corruption detected in the source file. A log file is only created if corruption is detected in the source file. If LogFile is False or omitted, no log file is created, even if corruption is detected in the source file.

因此,destDB 是压缩数据库。

通过几行额外的行,我可以达到预期的效果。首先,使用os.remove() 删除原始的、未压缩的数据库srcDB。然后使用os.rename() 将新数据库重命名为destDB,使其与原来的名称相同。

因此,完整的代码块如下所示:

import os
import win32com.client

srcDB = r'C:\My\Database\Path\My_Db.accdb'
destDB = r'C:\My\Database\Path\My_Db_PyBackup.accdb'

oApp = win32com.client.Dispatch("Access.Application")
oApp.compactRepair(srcDB, destDB)
oApp.Application.Quit()
oApp = None

os.remove(srcDB)
os.rename(destDB, srcDB)

因此最终得到一个已压缩和修复的数据库,其文件名与原始文件名相同。

【讨论】:

我们使用了更多的步骤。 0. 检查是否没有用户使用 srcDB 1. 将 srcDB 压缩到 destDB。 2. 检查 destDB 是否存在 2. 将 srcDB 重命名为 tmpDB 3. 检查 tmpDB 是否存在 4. 删除 srcDB 5. 检查 srcDB 是否存在 6. 将 destDB 移动到 srcDB 7. 删除 tmpDB.

以上是关于如何使用 Python 3 压缩和修复 Access 数据库的主要内容,如果未能解决你的问题,请参考以下文章

如何在使用 imagemin 通过电子应用程序压缩图像时修复以下文件错误?

单击“压缩和修复数据库”后,如何阻止 vba 编辑器中的 MS 访问断点消失?

[在第二次尝试刷新令牌时如何修复“格式错误的身份验证代码”?

gzip压缩文件损坏修复原理和数据恢复方法

如何使用 Python 3 将 lzma2 (.xz) 和 zstd (.zst) 文件解压缩到文件夹中?

如何在 python 3.4 tkinter“索引错误:列表索引超出范围”中修复此错误