如何从 Python 脚本压缩和修复 Access 数据库 [英] How to compact and repair an Access database from a Python script

查看:41
本文介绍了如何从 Python 脚本压缩和修复 Access 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 ArcGIS 自定义工具箱运行的 Python 脚本.该脚本处理 Access 数据库(主要是个人地理数据库).我想知道的是,有没有办法从我的 python 脚本压缩和修复访问数据库(特别是个人 gdb)?

I have a python script running from an ArcGIS custom toolbox. The script deals with Access databases (mostly personal geodatabases). What I am wondering is, is there a way to compact and repair an access database (specifically a personal gdb) from my python script?

预先感谢您的任何帮助和建议,

Thanks in advance for any help and advice,

本.

推荐答案

使用 Python 的 win32com 库和调用 VBA 方法 CompactRepair().请注意,在此过程中需要备份文件路径(尚不存在的路径),但可以在成功压缩后删除,使用 os.remove() 满足该需要:

Use Python's win32com library and make a call to the VBA method CompactRepair(). Do note, a backup file path is required (one that does not exist yet) during the process but can be deleted after successful compact, using os.remove() for that need:

import os
import win32com.client

srcDB = 'C:\\Path\\To\\Database.accdb'
destDB = 'C:\\Path\\To\\Database_backup.accdb'

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

os.remove(destDB)
oApp = None  

这篇关于如何从 Python 脚本压缩和修复 Access 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆