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