Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load mysql dump to hsqldb database?

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
like image 779
Skarab Avatar asked Sep 28 '10 10:09

Skarab


People also ask

How do I connect to Hsqldb in-memory?

Because we started an in-memory instance the connection url is a memory database url that looks like jdbc:hsqldb:mem:instanceName You will not be able to connect using this url because neither the host and the port are available, instead you should use a server database url that looks like jdbc:hsqldb:hsql://host:port/ ...

What is Hsqldb in-memory database?

HSQLDB (HyperSQL Database) HSQLDB is an open source project, also written in Java, representing a relational database. It follows the SQL and JDBC standards and supports SQL features such as stored procedures and triggers. It can be used in the in-memory mode, or it can be configured to use disk storage.


2 Answers

  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();
    
like image 157
Skarab Avatar answered Oct 01 '22 03:10

Skarab


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 ,
like image 31
Antoine Avatar answered Oct 01 '22 01:10

Antoine