November 18, 2010

My sqllite helper builder

EDIT 11/11/2012: as described in this new blog post, I modified the script to generate a content provider as well

This is my first post about something related to android.
Just to break the ice, I would like to introduce a simple helper script I developed this summer.

Android ships with a very nice local storage, which is sqllite. It's said almost everywhere that, in order to make your life easier, you should develop an helper class.
It will take care of open / close the sqllite db, of the version control and of any other kind of interaction you may have with it.

A nice example can be found in the official android documentation here
or here.
Reto Meier in his excellent "Professional Android Development" explains the same, and I am pretty sure its the same for other manuals.

Well, building a sqllite helper is BOOORING. You always have to do the same things, write the same functions of every table you want to store in it.
And here is where my script comes in your help.
You just provide it a configuration file with the description of the tables you want to store in it, and it will give you the dbhelper java file.

A configuration file looks like this:

CLASS Employee
String Name
Date Birthday
Double weigth

String Number
Date Time

The available types are
String, Double, Long, Integer and Date

The available script commands are:

 -h, --help            show this help message and exit
-i INFILE, --infile=INFILE
file that contains classes definition
-n NAME, --name=NAME name of the dbhelper class
-p PACKAGE, --package=PACKAGE
name of the package

So, instead of spending an entire afternoon writing the same add / remove / getAll methods, all you have to do is to compile the configuration file, launch something like

python -i sample.txt -n MyDbAdapter -p

and you'll have your brand new sqllite helper class. Of course you can extend it, for example if you want to have some insert / get methods that accept and return java classes instead of fields, but in any case it will save you a lot of time.

An example of the output file can be found here.

The script is available on github here.

I really hope it can help somebody. If you have any issues / requests, feel free to contact me at fedepaol[AT]gmail[DOT]com .
comments powered by Disqus