我正在使用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
- 我总是负。如何正确比较数据结构?
https://stackoverflow.com/questions/46709777/which-jar-files-are-required-for-schemacrawler –
Sualeh的重复,不完全是。在你指出的那个问题中,我问的是为什么它没有连接。在这个例子中,我正在询问如何使用SchemaCrawler或任何其他插件来实现它的示例。不幸的是,可用的例子并不多。 – Alyona
如何比较两个数据库结构与SchemaCrawler和**最重要**是否有可能**将数据从一个数据库复制到其他**,如果没有数据的某些列只是让它们为空? – Alyona