GreenDoa Getting Started

Advertisement
GreenDoa Getting Started

GreenDoa Getting Started

Adding library to android project

In build.gradle

// In your root build.gradle file:
buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}
 
// In your app projects build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
 
dependencies {
    implementation 'org.greenrobot:greendao:3.2.2' // add library
}

initialize the GreenDoa

In your application class you can initialize the GreenDoa instance and save it in global scope

//setup green doa  
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "formionax-encrypted" : "formionax");  
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();  
DaoSession daoSession = new DaoMaster(db).newSession();  
Global.daoSession=daoSession;

Create your table model

Create your class file which will be your table model
FileSyncModel.java

import org.greenrobot.greendao.annotation.Entity;  
import org.greenrobot.greendao.annotation.Id;  
  
@Entity  
public class FileSyncModel {  
  
    @Id(autoincrement = true)  
    public Long id;  
  
    String fileName="";  
    String fileAbsolutePath="";  
    String fileType="";  
    String sync="false";  
  
}

Now re-build your project your class will be modified by greenDoa and will look like

  
import org.greenrobot.greendao.annotation.Entity;  
import org.greenrobot.greendao.annotation.Id;  
import org.greenrobot.greendao.annotation.Generated;  
  
@Entity  
public class FileSyncModel {  
  
    @Id(autoincrement = true)  
    public Long id;  
  
    String fileName="";  
    String fileAbsolutePath="";  
    String fileType="";  
    String sync="false";  
    @Generated(hash = 900230584)  
    public FileSyncModel(Long id, String fileName, String fileAbsolutePath,  
            String fileType, String sync) {  
        this.id = id;  
        this.fileName = fileName;  
        this.fileAbsolutePath = fileAbsolutePath;  
        this.fileType = fileType;  
        this.sync = sync;  
    }  
    @Generated(hash = 1434501029)  
    public FileSyncModel() {  
    }  
    public Long getId() {  
        return this.id;  
    }  
    public void setId(Long id) {  
        this.id = id;  
    }  
    public String getFileName() {  
        return this.fileName;  
    }  
    public void setFileName(String fileName) {  
        this.fileName = fileName;  
    }  
    public String getFileAbsolutePath() {  
        return this.fileAbsolutePath;  
    }  
    public void setFileAbsolutePath(String fileAbsolutePath) {  
        this.fileAbsolutePath = fileAbsolutePath;  
    }  
    public String getFileType() {  
        return this.fileType;  
    }  
    public void setFileType(String fileType) {  
        this.fileType = fileType;  
    }  
    public String getSync() {  
        return this.sync;  
    }  
    public void setSync(String sync) {  
        this.sync = sync;  
    }  
  
}

Lets run some query

  • Get the model schema instance
static FileSyncModelDao mFileSyncModelDao = Global.daoSession.getFileSyncModelDao();
  • Insert records in the model
FileSyncModel tmpFileSyncModel = new FileSyncModel();
tmpFileSyncModel.setFileAbsolutePath("filepath");
tmpFileSyncModel.setFileType("image");
tmpFileSyncModel.setFileName("fileName");
mFileSyncModelDao.save(tmpFileSyncModel);
  • Get all the records where sync is false
QueryBuilder<FileSyncModel> qb = mFileSyncModelDao.queryBuilder();  
qb.where(FileSyncModelDao.Properties.Sync.eq("false"));  
//get the result in arrary list now access form model to get the data  
ArrayList<FileSyncModel> fileSyncModelArrayList = (ArrayList<FileSyncModel>) qb.list();  
Log.wtf("SKDINFO","files to be sync "+fileSyncModelArrayList.size());
  • update the above records.
FileSyncModel tmpFileSyncModel = fileSyncModelArrayList.get(0);
tmpFileSyncModel.setSync("true");  
mFileSyncModelDao.update(tmpFileSyncModel);
  • Running multiple where queries, Since Single query is not enough
QueryBuilder<FileSyncModel> qb = mFileSyncModelDao.queryBuilder();
qb.where(qb.and(FileSyncModelDao.Properties.Sync.eq("false"),FileSyncModelDao.Properties.FileType.eq("image")));
//get the result in arrary list now access form model to get the data  
ArrayList<FileSyncModel> fileSyncModelArrayList = (ArrayList<FileSyncModel>) qb.list();  
Log.wtf("SKDINFO","files to be sync "+fileSyncModelArrayList.size());

As you have seen how simple is to manage our sqlite database in android using GreenDoa
Follow http://greenrobot.org/greendao/documentation/introduction/ official documentation for more info

Advertisement

Comments (3)

  1. For readers exploring a new platform and looking for a simple way to understand how things work, a clear getting-started guide can be useful before moving on to other online information resources. If you want to check lottery updates and results, you can find the latest information here on DearLotteryIN.

    ReplyDelete
    Replies
    1. The tutorial provides a practical introduction to integrating GreenDAO into an Android project, covering Gradle configuration, plugin setup, database initialization, and entity creation. The example shows how DaoMaster, DaoSession, and DevOpenHelper work together to establish database access, while the annotated FileSyncModel demonstrates how a Java class can represent a database table.

      The step-by-step setup is useful for understanding how Android applications can manage structured local data through an ORM-style persistence layer. Developers exploring modern cross-platform mobile development can build on these concepts with a React Native Course, particularly when considering how application data and persistence requirements translate across different mobile development approaches.

      Delete
    2. The FileSyncModel example also shows how application-specific entities such as filenames, paths, file types, and synchronization status can be organized for local storage. Students interested in extending such database-driven functionality into complete applications can explore Android Projects for Final Year as a practical direction for building Android applications with local data management and synchronization features.

      Delete