How to compress an MS Access database

前端 未结 4 1189
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 22:25

I have an .mdb file which is 70MB.

After deleting all records contained in the file, the size remains 70MB.

How do I make my .mdb f

4条回答
  •  抹茶落季
    2021-01-11 22:41

    The Microsoft Access database engine provides a CompactDatabase method that makes a compact copy of the database file. The database file must be closed before calling CompactDatabase.

    Documentation:

    • Pages on microsoft.com about "Compact and Repair Database"
    • DBEngine.CompactDatabase Method (DAO)

    Here's a Python script that uses DAO to copy and compact MDB files:

    import os.path
    import sys
    import win32com.client
    
    # Access 97: DAO.DBEngine.35
    # Access 2000/2003: DAO.DBEngine.36
    # Access 2007: DAO.DBEngine.120
    daoEngine = win32com.client.Dispatch('DAO.DBEngine.36')
    
    if len(sys.argv) != 3:
        print("Uses Microsoft DAO to copy the database file and compact it.")
        print("Usage: %s DB_FILE FILE_TO_WRITE" % os.path.basename(sys.argv[0]))
        sys.exit(2)
    
    (src_db_path, dest_db_path) = sys.argv[1:]
    print('Using database "%s", compacting to "%s"' % (src_db_path, dest_db_path))
    daoEngine.CompactDatabase(src_db_path, dest_db_path)
    print("Done")
    

提交回复
热议问题