0

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

Kaliklipper
  • 355
  • 5
  • 19
  • myself, I need to initialize a single table in a database, not the whole database, so the answer given here isn't helping. I'll look further. but why not just read the sql file line by line and execute? — given you can guarantee that the file has one statement per line? – mariotomo Jan 30 '18 at 12:25

1 Answers1

0

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.

yegodm
  • 1,014
  • 7
  • 15
  • Thanks Yegodom, I have used that technique before and used a make file to rebuild it from a sql file to create the tables and load some standing data. Then had the app copy it across on launch to its run directory. I was wondering if I could do something similar with maven to build it automatically in intelliJ. I'll take a look at the ant plugin and see if I can do something on the cmd line from maven... – Kaliklipper Aug 30 '17 at 19:23
  • @BradleyAtkins Alternatively you can probably give a try to [maven-resources-plugin](https://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html). – yegodm Aug 30 '17 at 19:39