How to extract the schema of an Access (.mdb) database?

后端 未结 10 2551
星月不相逢
星月不相逢 2020-11-30 19:39

I am trying to extract the schema of an .mdb database, so that I can recreate the database elsewhere.

How can I pull off something like this?

10条回答
  •  难免孤独
    2020-11-30 20:04

    If you're happy to use something other than pure Access SQL, you could persist a collection of ADOX objects and use those to recreate the table structure.

    Example (in Python, doesn't currently recreate relationships and indexes as it wasn't needed for the project I was working on):

    import os
    import sys
    import datetime
    import comtypes.client as client
    
    class Db:
        def __init__(self, original_con_string = None, file_path = None,
                     new_con_string = None, localise_links = False):
            self.original_con_string = original_con_string
            self.file_path = file_path
            self.new_con_string = new_con_string
            self.localise_links = localise_links
    
        def output_table_structures(self, verbosity = 0):
            if os.path.exists(self.file_path):
                if not os.path.isdir(self.file_path):
                    raise Exception("file_path must be a directory!")
            else:
                os.mkdir(self.file_path)
            cat = client.CreateObject("ADOX.Catalog")
            cat.ActiveConnection = self.original_con_string
            linked_tables = ()
            for table in cat.Tables:
                if table.Type == u"TABLE":
                    f = open(self.file_path + os.path.sep +
                             "Tablestruct_" + table.Name + ".txt", "w")
                    conn = client.CreateObject("ADODB.Connection")
                    conn.ConnectionString = self.original_con_string
                    rs = client.CreateObject("ADODB.Recordset")
                    conn.Open()
                    rs.Open("SELECT TOP 1 * FROM [%s];" % table.Name, conn)
                    for field in rs.Fields:
                        col = table.Columns[field.Name]
                        col_details = (col.Name, col.Type, col.DefinedSize,
                                       col.Attributes)
                        property_dict = {}
                        property_dict["Autoincrement"] = (
                            col.Properties["Autoincrement"].Value)
                        col_details += property_dict,
                        f.write(repr(col_details) + "\n")
                    rs.Close()
                    conn.Close()
                    f.close()
                if table.Type == u"LINK":
                    table_details = table.Name,
                    table_details += table.Properties(
                        "Jet OLEDB:Link DataSource").Value,
                    table_details += table.Properties(
                        "Jet OLEDB:Link Provider String").Value,
                    table_details += table.Properties(
                        "Jet OLEDB:Remote Table Name").Value,
                    linked_tables += table_details,
            if linked_tables != ():
                f = open(self.file_path + os.path.sep +
                         "linked_list.txt", "w")
                for t in linked_tables:
                    f.write(repr(t) + "\n")
            cat.ActiveConnection.Close()
    

    A similar reverse function reconstructs the database using the second connection string.

提交回复
热议问题