Load a SQLite3 database from a .sql file using java sqlite-jdbc

1.3k views Asked by At

I'm trying to put together a simple project that uses a SQLite3 database and I'm planning to use the sqlite-jdbc driver for it.

I would prefer to create the database from a .sql file, rather than explicit commands in Java though.

I could just create the file and manually direct the file into the database like so:

sqlite3 mydb < my file.sql

but I would rather do it from code, perhaps by executing the .read command, or even better via maven, (which I am also learning). So that it would be created fresh every time I built the project.

If anyone could advise me on how to do either it would be appreciated.

Thanks in advance

Brad

1

There are 1 answers

2
yegodm On

You may consider storing your mydb file as a database template. Once created with the SQL-script as you described, keep it intact somewhere in the project structure. Every time you need a fresh database you can simply copy this template to the location where your application expects it to find.