如何将 mysql 转储加载到 hsqldb 数据库?

发布于 2024-09-25 16:28:38 字数 2829 浏览 1 评论 0原文

我有一个在 mysql 中创建数据库的 sql 文件:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`machine`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`machine` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

现在我想将此文件加载到 hsqldb 2 数据库中。我需要在 mysql 转储中更改什么才能将数据加载到 hsqldb 中?

目前我使用这段代码(groovy)来执行sql文件:

def embeddedDbSettings = [url:'jdbc:hsqldb:file:mydb', user:'sa', password:'', driver:'org.hsqldb.jdbcDriver'];
sql =  Sql.newInstance(embeddedDb);
sql.executeInsert new File("./sql/create_database.sql").text;

并且我一直得到这个加密异常:

Exception in thread "main" java.sql.SQLException: unknown token
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
    at groovy.sql.Sql.executeInsert(Sql.java:1440)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:229)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at de.hpi.ecir.eval_script.Convert2Excel.main(Convert2Excel.groovy:37)
Caused by: org.hsqldb.HsqlException: unknown token
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.ParserBase.read(Unknown Source)
    at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 13 more

I have a sql file that creates a database in mysql:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`machine`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`machine` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

Now I would like to load this file into hsqldb 2 database. What do I need to change in the mysql dump to load the data into hsqldb?

Currently I use this code (groovy) to execute the sql file:

def embeddedDbSettings = [url:'jdbc:hsqldb:file:mydb', user:'sa', password:'', driver:'org.hsqldb.jdbcDriver'];
sql =  Sql.newInstance(embeddedDb);
sql.executeInsert new File("./sql/create_database.sql").text;

and all the time I got this crypting exception:

Exception in thread "main" java.sql.SQLException: unknown token
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
    at groovy.sql.Sql.executeInsert(Sql.java:1440)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:229)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at de.hpi.ecir.eval_script.Convert2Excel.main(Convert2Excel.groovy:37)
Caused by: org.hsqldb.HsqlException: unknown token
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.ParserBase.read(Unknown Source)
    at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 13 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半山落雨半山空 2024-10-02 16:28:38
  1. 删除所有 SET 行将
  2. 创建数据库的命令更改为:CREATE SCHEMA mydb AUTHORIZATION DBA
  3. 删除所有如果不存在 - hsqldb 不支持此命令
  4. 删除所有推荐(不是必需的,但您在本文中找到的代码需要)
  5. 删除所有 `
  6. 将 TINYINT (相当于布尔值的 mysql)替换为布尔值
  7. 单独执行每个命令:

    String[] 命令 = new File("./sql/create_database.sql").text.split(";");
    
    for(字符串命令:命令)
    {
    
     // 新行是hsqldb中的分隔符
    
      sql.执行命令.replace("\n", " ");
    }
    
    // 记得调用 shutdown 否则 hsqldb 将不会保存你的数据
    sql.执行“关闭”
    sql.close();
    
  1. Remove all SET lines
  2. Change a line with command which creates a database to: CREATE SCHEMA mydb AUTHORIZATION DBA
  3. Remove all if not exists - hsqldb does not support this command
  4. Remove all commends (not neccesary but needed for the code you find in this post)
  5. Remove all `
  6. Replace TINYINT (mysql equivalent for boolean) by boolean
  7. Execute each command separately:

    String[] commands = new File("./sql/create_database.sql").text.split(";");
    
    for(String command: commands)
    {
    
     // new line is a delimiter in hsqldb
    
      sql.execute command.replace("\n", " ");
    }
    
    // remember to call shutdown otherwise hsqldb will not save your data
    sql.execute "SHUTDOWN"
    sql.close();
    
指尖凝香 2024-10-02 16:28:38

您还必须:

  • 将 CREATE_TABLE 中的“AUTO_INCRMENT”替换为“GENERATED BY DEFAULT AS IDENTITY”,
  • 将“int”替换为“integer”,
  • 在列创建中移动“default”语句,例如:

从 this :

CT_CLIENT integer NOT NULL DEFAULT '0',

到 this :

CT_CLIENT integer DEFAULT '0' NOT NULL ,

You also have to :

  • replace "AUTO_INCREMENT" in CREATE_TABLE by "GENERATED BY DEFAULT AS IDENTITY"
  • replace "int" by "integer"
  • move "default" statement in column creation for example :

from this :

CT_CLIENT integer NOT NULL DEFAULT '0',

to this :

CT_CLIENT integer DEFAULT '0' NOT NULL ,
叶落知秋 2024-10-02 16:28:38

您不必单独运行每个命令,如果您一次运行所有脚本,只要所有令牌都有效,hsqldb 就可以正常工作。

You don't have to run each command separately, hsqldb works fine if you run the scripts all at once, as long as all your tokens are valid.

囍孤女 2024-10-02 16:28:38

使用 IntelliJ IDEA 解决了这个问题:

  1. 在数据库选项卡中,添加到数据库的连接(在本例中为 MySQL),
  2. 右键单击所需的数据库,然后单击“复制 DDL”。

Solved this problem using IntelliJ IDEA :

  1. In the database tab, add a connection to your database (MySQL in this case)
  2. Right-click on desired database and click on "Copy DDL".
情泪▽动烟 2024-10-02 16:28:38

我依靠 RazorSQL 解决了这个问题。它不是免费的,但使用评估版本,您足以执行从 MySQL 到 HSQLDB 的转换。它还支持其他数据库转换。

我在转换过程中检测到的唯一问题是主键。所以基本上,以下生成的代码摘录不会为我运行:

CREATE TABLE items_fractions (
  id INTEGER IDENTITY NOT NULL,
  item_id INTEGER NOT NULL,
  fraction_id INTEGER NOT NULL,
  PRIMARY KEY (id)
);

我必须删除 IDENTITY 位。

I solved this issue by relying on RazorSQL. It is not for free, but with the evaluation version you have enough for performing the conversion from MySQL to HSQLDB. It also supports other DB conversions.

The only problem I detected during the conversion was the primary keys. So basically, the following generated code excerpt would not run for me:

CREATE TABLE items_fractions (
  id INTEGER IDENTITY NOT NULL,
  item_id INTEGER NOT NULL,
  fraction_id INTEGER NOT NULL,
  PRIMARY KEY (id)
);

I had to remove the IDENTITY bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文