2017-10-12 89 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