【发布时间】:2017-10-12 10:11:44
【问题描述】:
我正在使用 SQLite/Hibernate。想法是每次应用启动时检查数据库结构是否是最新的。我在“DB”文件夹中有我现有的数据库,每次应用程序启动时,我都会在“DB/structure”文件夹中创建最新的数据库。
我想比较它们,如果我现有的数据库是旧的,请将数据复制到最新的数据库。摆脱旧数据库并移动新的数据库。
到目前为止,我已经尝试过 SchemaCrawler,但我遇到了错误,无法弄清楚。
更新:
我用 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 - 我总是得到否定的答案。如何正确比较数据结构?
【问题讨论】:
-
Sualeh,不完全是。在那个问题中,你指出,我在问为什么它没有连接。在这篇文章中,我正在询问如何使用 SchemaCrawler 或任何其他插件来完成此操作的示例。不幸的是,可用的例子并不多。
-
我如何将两个数据库结构与 SchemaCrawler 进行比较,最重要是否可以将数据从一个数据库复制到另一个数据库,如果没有某些列的数据只是将它们留空?
标签: java database hibernate sqlite schemacrawler