def win_compact_mdb(mdb_path, compacted_mdb_path, sort_order = "General\0\0"):
if sys.platform not in ('win32','cli'):
raise Exception('This function is available for use in Windows only.')
mdb_driver = [d for d in drivers() if 'Microsoft Access Driver (*.mdb' in d]
if mdb_driver == []:
raise Exception('Access Driver is not found.')
else:
driver_name = mdb_driver[0].encode('mbcs')
#COMPACT_DB=<source path> <destination path> <sort order>
ctypes.windll.ODBCCP32.SQLConfigDataSource.argtypes = [ctypes.c_void_p,ctypes.c_ushort,ctypes.c_char_p,ctypes.c_char_p]
#driver_name = "Microsoft Access Driver (*.mdb)"
if py_v3:
c_Path = bytes("COMPACT_DB=" + mdb_path + " " + compacted_mdb_path + " " + sort_order,'mbcs')
#driver_name = bytes(driver_name,'mbcs')
else:
c_Path = "COMPACT_DB=" + mdb_path + " " + compacted_mdb_path + " " + sort_order
ODBC_ADD_SYS_DSN = 1
ret = ctypes.windll.ODBCCP32.SQLConfigDataSource(None,ODBC_ADD_SYS_DSN,driver_name, c_Path)
if not ret:
raise Exception('Failed to compact Access mdb file - "%s". Please check file path, permission and Access driver readiness.' %compacted_mdb_path)
评论列表
文章目录