ANDROID SQLITE DATABASE WITH GREENDAO- V3

As times change things are came out from browser to the mobile app. In browser there is auto form filled or save password kind of things which stores your information inside browser cache or cookies or session. Same goes with the mobile application but in bit different way.

I will try to explain things which are important to consider when you need to store information inside your application and not on the server.

Things to consider like –

  • why would we need such features .
  • when do we need such features.
  • what is the benefit of using such features.
  • what are the options available.

To answer all your wh? Let me put you into a situation where you need to record couple of survey while you are offline and push those survey back to the server once you are online. If you are developing a game and you need to store the user score, their coins or badges. This is quite a common situation for mobile app. To handle this situation android comes with an option to create a database same as you create on any server. Where you store relevant information.

You can accomplish this task using android.database.sqlite, an android package with some helper classes or from third-party ORM like Green Dao as well.

If you would like to go with the android official way you might find following link useful https://developer.android.com/training/basics/data-storage/databases.html. However if you wanna experiment with ORM and code like a pro then experience with GreenDao will remain frozen in your memory for the long time. Its open source and keeps things very simple and straight forward. all you need is to define your tables and their columns with their data type and when you run that class it will automatically creates entity and database dao classes for you.

See GreenDao In Action

  1. Create new android project
  2. Add gradle dependency in application gradle file.
    compile 'net.zetetic:android-database-sqlcipher:3.5.6'// this library ensures that your DB is safe and encrypted
    
    compile 'org.greenrobot:greendao:3.2.2'// add library
  3. Add gradle dependency in project gradle file.classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2// add library
  4. Add this code on top of your application gradle file
    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao'
    
    android {
  5.  Put this code before dependency section in your application gradle file.
    greendao {
        schemaVersion 1 // this will be like your database version
        targetGenDir "src/main/java/" // this path ensures that you have the database package at this path 
    and will create other Dao and Entity class at the same path
    }
    
    
    dependencies {
    
  6. Create a database package in your root package.
  7. Create a Java/Entity class for your database table. like – user. Make sure to annotate your class with @Entity tag.  Like this.
    @Entity
    public class User {
    
        // this will make your id autoincremented
        @org.greenrobot.greendao.annotation.Id (autoincrement = true)
        private Long Id;
        private String name; // Whatever column you want in your table.
  8. When all is done. Go to the build menu and click on make project option. 
    This will generate the Dao classes and Dao Session for your entity.
  9. 
    

    Now, Make an ApplicationClass and put this code into class body.
    /** A flag to show how easily you can switch from standard SQLite to the encrypted SQLCipher. */
    public static final boolean ENCRYPTED = true;

    private DaoSession daoSession;

    @Override
    public void onCreate() {
    super.onCreate();

    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? “your-db-encrypted” : “your-db”);
    Database db = ENCRYPTED ? helper.getEncryptedWritableDb(“DB-Name”) : helper.getWritableDb();
    daoSession = new DaoMaster(db).newSession();
    }

    public DaoSession getDaoSession() {
    return daoSession;
    }

  10. Map your application class into the manifest file.
    <application
        android:name=".activities.MyApplication"
  11.  Now, whenever you want to make a database operation. follow this step

    define the dao and entity class into variable

    private UserDao userDao;
    private User user;
    Call Dao session from application class
    // get the note DAO
    DaoSession daoSession = ((MyApplication) getApplication()).getDaoSession();
    userDao = daoSession.getUserDao();
    Once you get the DaoSession 
    Initialize and assign the value into entity class variable.
    user = new User();
    user.setName(message.get("name"));
    user.setNumber(message.get("number"));
    call this line to either save or update the record.
    Long id = userDao.insertOrReplace(user);

    i personally had very tough time setting up the GreenDao 3 with android studio version 3. I hope that this will help you to set up the latest greenDao into your android application.

    Download the working example from GitHub GreenDao ORM Implementation.