I'm writing a Java application that uses SQLite via JDBC for persistence.
What I need to do when the application starts is:
In order to create the DB I thought of externalizing the SQL commands needed to create the structure (e.g. CREATE TABLE ...) in an external file, load it at runtime and execute it.
I thought of using BufferedReader
with .readLine()
and then feed each SQL command to a Statement.executeUpdate()
. Is there some smarter way to do this?
Concerning the startup check of the database schema, while I think that this should be considered "good design", I don't know if in practice it could be overkill or useless. One solution I came up with but has the disadvantage of being "vendor dependent" is this:
nwe_
) sqlite_master
table with WHERE name LIKE 'nwe_%'
SQL
column with the SQL command file I used for creating the DBAgain, is there some smarter way to do this? (maybe not "vendor dependent", but this at the time is not much of an issue for me)
Thanks.
You can write up your own verification of the database by looking in the metdata tables. This is not impossible, but it is a lot of code to maintain. You can also write up a lot of DDL statements to construct the database structures, again not impossible, but a lot of code to maintain.
If you follow such a path, I recommend a higher-level logic of
if (!checkDatabase()) {
try {
if (wantsToInstall()) {
installDatabase();
}
} catch (Exception e) {
exit();
}
}
or a specific installer (to reduce the chance of installing over existing items). Extensions that provide deleting data items can be done, but there are differing opinions as to what to do if encountering an unexpected pre-existing object during install / upgrade.
Now there are a few libraries that will do this for you; but by the time they add this feature, such a library probably takes care of much more than simple database structure maintenance. Look to JDO and a few others, I know that DataNucleus's environment will gladly create database structures tweaked to work well in just over 20 different databases (remember they all have subtle differences in how they do things).