Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing script file in h2 database

Tags:

sql-scripts

h2

First of all I would like to say am new to h2 database. I need to execute a sql script file in h2 database. I have a script file test.sql and I want to execute this in h2 database. Is it possible?

like image 401
Kamalam Avatar asked May 20 '12 17:05

Kamalam


People also ask

How do I access H2 DB files?

Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .

How to install and work with H2 database?

To install and work with H2 database, find the steps. 1. Go to official website link. Download and install in your computer. 2. Open H2 Console (Command Line) and access URL http://localhost:8082 in browser. Select server mode and in JDBC URL we can change 'test' database name as we want. In our example we will change database name as 'mydb'.

How to read data from CSV file and write into H2 database?

H2 database is the open source Java SQL database. In our example we will create a batch application which will read data from a CSV file and write into H2 database. In our Spring Batch application, we will use FlatFileItemReader to create reader and JdbcBatchItemWriter to create writer.

What is the logger name of h2database?

The logger name is h2database . If it does not work, check the file <database>.trace.db for error messages. If the database files are read-only, then the database is read-only as well. It is not possible to create new tables, add or modify data in this database.

How do I encrypt the database test in the H2 console?

The tool is available from within the H2 Console in the tools section, or you can run it from the command line. The following command line will encrypt the database test in the user home directory with the file password filepwd and the encryption algorithm AES:


2 Answers

You can use the RUNSCRIPT SQL statement:

RUNSCRIPT FROM 'test.sql' 

or you can use the RunScript standalone / command line tool:

java -cp h2*.jar org.h2.tools.RunScript -url jdbc:h2:~/test -script test.sql 

You can also use the RunScript tool within an application:

RunScript.execute(conn, new FileReader("test.sql")); 
like image 158
Thomas Mueller Avatar answered Sep 21 '22 15:09

Thomas Mueller


If you are using spring-boot and spring-test with H2 it will automatically look for schema.sql and data.sql in your class path and attempt to run these. So if you put them in src/test/resources they should be picked up and run automatically

In addition you can specify the data files you want to run with properties. For example adding a property to yourapplication.properties like

spring.datasource.data=classpath:users.sql, classpath:books.sql, classpath:reviews.sql 

will configure spring to run those three sql files instead of running data.sql

like image 36
robjwilkins Avatar answered Sep 22 '22 15:09

robjwilkins