Pure Java DB implementation

Hi Levino!

In my project I did not like to think to build more dependencies on android. I thought: “It’ll be cooler to be as close to pure java, instead of having dependencies on Android AND iOS at the same time”. So I only use platform dependent SQLite3 connections by using SQLdroid on android and ios and passing the opened driver connection down to the core-module of the app. This way I can also test the integrity of the database with pure java while staying on my development host-machine.

In my core module I can then easly just use the default SQLite classes of java. In my initializing code on the platforms I just had to select the correct db driver then:

on iOS it would look something like this:

DriverManager.registerDriver((Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance());
NSArray<String> paths = Foundation.NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true);
String documentsDirectory = paths.get(0);
String url = "jdbc:sqldroid:" + documentsDirectory+"/database.db";
connection = DriverManager.getConnection(url);

on Android it would look like this:

DriverManager.registerDriver((Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance());
String url = "jdbc:sqldroid:" + context.getFilesDir().getPath() + "/database.db"
connection = DriverManager.getConnection(url);

When you reform it to make it a bit more abstract you then can just pass the connection instance to your database service.

And when you test on your local machine you are also able to use:

// register your db driver
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:database.db";

… and so on.

Hope this helps a bit!

2 Likes