JavaStorm

Getting started with SQLite Android

Adding to Project

To add storm to your Android project, please use below for Gradle in your project specific Gradle file. This would add Cubestack bitray Mave repository to your project

maven { url "http://dl.bintray.com/cubestack/maven" }

Once Maven repository is added, you may import Storm to your project with current version (1.0g) or any other

dependencies {
    compile 'in.cubestack.android.lib:storm:1.0g'
}

Getting Started

Storm needs to know the basic database details and the tables it needs to manage.
Three basic steps:

Step One (Define the Table)

Tables can be defined easily with marking your entities with annotations similar to most ORMs

@Table(name = "DEMO_ENTITY")
class Entity {
   @PrimaryKey
   @Column(name="ID", type = FieldType.INTEGER)
   private int id;
 }

Once you have tables defined as above, the next step is to bind them to a database.

Step Two (Define the Database)

Database also have their annotation which can be applied, there can be multiple databases defined.

@Database(name="MY_DB", tables = {Entity.class, AnotherEntity.class}, version = 2)
class Database {}

Step Three (Start Using)

With database ready for us, we may start using it as below:
Retrival

StormService service = new BaseService(getContext(), Database.class);
List<Entity> savedEntities  = service.findAll(Entity.class);

Save

StormService service = new BaseService(getContext(), Database.class);
Entity entity = new Entity ();
// Set all values
service.save(entity);
Assert.assertTrue(entity.id > 0 );

Not only storm would save the entity for you, it would also auto increment the ID (You may override this behaviour with @PrimaryKey annotation), it would also update the new ID value generated to your entity.

That’s it

No other configurations, no create tables, nothing required. We donot want an entry in AndroidManifest, or any String to be declared in the XML.
We would read the entities defined as per the @Database annotation and make sure we make everything ready.

Leave a Reply

Your email address will not be published. Required fields are marked *