简体   繁体   English

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

[英]How to load mysql dump to hsqldb database?

I have a sql file that creates a database in mysql: 我有一个sql文件,在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. 现在我想将此文件加载到hsqldb 2数据库中。 What do I need to change in the mysql dump to load the data into hsqldb? 我需要在mysql转储中更改以将数据加载到hsqldb中?

Currently I use this code (groovy) to execute the sql file: 目前我使用此代码(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;

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
  1. Remove all SET lines 删除所有SET行
  2. Change a line with command which creates a database to: CREATE SCHEMA mydb AUTHORIZATION DBA 使用创建数据库的命令更改一行: CREATE SCHEMA mydb AUTHORIZATION DBA
  3. Remove all if not exists - hsqldb does not support this command if not exists删除所有 - hsqldb不支持此命令
  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 用布尔值替换TINYINT(boolean的mysql等价物)
  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(); 

You also have to : 你还必须:

  • replace "AUTO_INCREMENT" in CREATE_TABLE by "GENERATED BY DEFAULT AS IDENTITY" 将CREATE_TABLE中的“AUTO_INCREMENT”替换为“默认为默认生成”
  • replace "int" by "integer" 将“int”替换为“integer”
  • move "default" statement in column creation for example : 例如,在列创建中移动“default”语句:

from this : 由此 :

CT_CLIENT integer NOT NULL DEFAULT '0',

to this : 对此:

CT_CLIENT integer DEFAULT '0' NOT NULL ,

Solved this problem using IntelliJ IDEA : 使用IntelliJ IDEA解决了这个问题:

  1. In the database tab, add a connection to your database (MySQL in this case) 在数据库选项卡中,添加与数据库的连接(在本例中为MySQL)
  2. Right-click on desired database and click on "Copy DDL". 右键单击所需的数据库,然后单击“复制DDL”。

I solved this issue by relying on RazorSQL . 我依靠RazorSQL解决了这个问题。 It is not for free, but with the evaluation version you have enough for performing the conversion from MySQL to HSQLDB. 它不是免费的,但是评估版本足以执行从MySQL到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. 我不得不删除IDENTITY位。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM