2017-10-12 52 views
0

我正在使用SQLite/Hibernate。想法是每次启动应用程序时检查数据库结构是否是最新的。我在“DB”文件夹中有我现有的数据库,每次应用程序启动时,我都会在“DB/structure”文件夹中创建最新的数据库。如何比较两个数据库结构?

我想比较他们,如果我现有的数据库是旧的,将数据复制到最新的数据库。摆脱旧的数据库,并在它的地方移动新的数据库。

到目前为止,我已经试过SchemaCrawler,但我得到了错误,并且无法弄清楚。

UPDATE:

我SchemaCrawler连接到这两个数据库:

public SchemaConroller() { 

     SchemaCrawlerOptions options = new SchemaCrawlerOptions(); 
     options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard()); 

     Catalog catalog1=null; 
     try { 
      Connection conn = getConnection(); 
      catalog1 = SchemaCrawlerUtility.getCatalog(conn, options); 
      for (Schema schema : catalog1.getSchemas()) { 
       System.out.println(schema); 
       for (Table table : catalog1.getTables(schema)) { 
        System.out.println("o--> " + table + " size: "+table.getColumns().size()); 
        /*for (Column column : table.getColumns()) { 
         System.out.println("  o--> " + column); 
        }*/ 
       } 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Catalog catalog2=null; 
     try { 
      Connection conn = getConnection2(); 
      catalog2 = SchemaCrawlerUtility.getCatalog(conn, options); 
      for (Schema schema : catalog2.getSchemas()) { 
       System.out.println(schema); 
       for (Table table : catalog2.getTables(schema)) { 
        System.out.println("o--> " + table + " size: "+table.getColumns().size()); 
        /*for (Column column : table.getColumns()) { 
         System.out.println("  o--> " + column); 
        }*/ 
       } 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if(catalog1.equals(catalog2)){ 
      System.out.println(">>>>>>>>>>>>DATABASE IS UP TO DATE<<<<<<<<<<<<<<"); 
     }else{ 
      System.out.println(">>>>>>>>>>>>DATABASE IS OUT OF DATE<<<<<<<<<<<<<<"); 
     } 
    } 

但我总是得到肯定的回答,如果我尝试catalog1 == catalog2 - 我总是负。如何正确比较数据结构?

+0

https://stackoverflow.com/questions/46709777/which-jar-files-are-required-for-schemacrawler –

+0

Sualeh的重复,不完全是。在你指出的那个问题中,我问的是为什么它没有连接。在这个例子中,我正在询问如何使用SchemaCrawler或任何其他插件来实现它的示例。不幸的是,可用的例子并不多。 – Alyona

+0

如何比较两个数据库结构与SchemaCrawler和**最重要**是否有可能**将数据从一个数据库复制到其他**,如果没有数据的某些列只是让它们为空? – Alyona

回答

0

Alyona,

请看一看schemacrawler-diff关于如何开始使用编程方式比较数据库结构开始的想法。请注意,此代码不是生产准备,也不支持,因此您将根据自己的需求开发自己的比较数据库的方法。

Sualeh Fatehi,SchemaCrawler

+0

谢谢你,下面的示例提供了我比较两个数据库的情况,但对于CHANGED值有点奇怪。在第二个数据库中,我只在表中添加了一列,但是我得到了1个'ADDED'和6'CHANGED'结果,即使我不记得改变任何东西,为什么它会如此反应: – Alyona

+0

'/ tables [flat] column flat was CHANGED to flat /tables[flat]/columns[flat.number] column null was added to flat.number /tables[flat]/columns[flat.owner_name] column flat.owner_name was CHANGED to flat.owner_name /tables [flat]/columns [flat.owner_surname]列flat.owner_surname已更改为flat.owner_surname /tables[flat]/columns[flat.area_size] column flat.area_size was changed to flat.area_size /tables [flat] /列[flat.people_living]列flat.people_living被更改为flat.people_living'等... – Alyona

+0

表格平面为空,但它显示任何列后添加一个更改。 – Alyona