Put Some SugarORM on Android

Mert SIMSEK
mobiwise blog

--

If you are developing android application, you will need to store your data on device. You have two way to go; Shared Preferences or SQLite DB. If you go with a quick solution, then i suggest you to use SharedPreferences. There are lots of good library about that. You can search it on github :)

This article will be more about second way to store data, SQLite. I think it will be better if I explain difference between SharedPreferences and SQLite. Basically, If you want to store tiny data like user preferences, Shared Preferences is a good option for you. But if you have big data set and you want to organize them in database then you need to create some helper classes to play with data.

Yes, couple of months ago, I used to create Helper classes like ;

But we devs people experience new things and try to make things easier. Using ORM libraries to store data seems like easiest way to go for now. There are good many ORM(Object Relational Mapping) libraries on Android.

For now, I only had experienced with Sugar ORM. For this reason, I am not going to push you to use Sugar ORM, not the others. I am just going to show you how Sugar ORM is easy.

Sugar ORM says that it is insanely easy. And believe me, it is. You can go through this link and check it on github.

Gradle Import

Add this to your build.gradle in app module.

compile 'com.github.satyan:sugar:1.3'

Create Database

This is a snippet from my project which i used Sugar ORM.

Use Sugar ORM

I am going to go through with an example, User.class

To save user to DB;

To load data;

To Update data;

To delete data;

List Operation;

Of course you can use entity relationship with sugar. And also another powerful feature is that you can create your own query and hit to the table.

You can find more information here. I wanted to keep this article as short as possible. If you have any question about sugarOrm , please don’t hesitate.

Thanks.

References

Github Page

Wiki Page

--

--