Coder Social home page Coder Social logo

greendao's Introduction

Andorid GreenDAO CRUD tutorial

Official website http://greenrobot.org/greendao/

Who is using greenDAO? http://www.appbrain.com/stats/libraries/details/greendao/greendao

ScreenShot

ScreenShot

4 Step setup project

Step 1

Open your root build.gradle add

    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
    }

Step 2

Open your build.gradle (Module: app) add

apply plugin: 'org.greenrobot.greendao'   //Must above apply plugin: 'com.android.application'
apply plugin: 'com.android.application'

greendao {
    schemaVersion 1
}

dependencies {
    compile 'org.greenrobot:greendao:3.2.0'
}

Step 3

Create class for extends Application

public class App extends Application {

    private DaoSession daoSession;

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

        MyDevOpenHelper helper = new MyDevOpenHelper(this,"notes-db");
        Database db = helper.getWritableDb();

        daoSession = new DaoMaster(db).newSession();
        instance = this;
    }

    private static App instance;

    public static App getInstance() {
        return instance;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}

Step 4

Create MyDevOpenHelper class extends DaoMaster.DevOpenHelper for update table

public class MyDevOpenHelper extends DaoMaster.DevOpenHelper{
    public MyDevOpenHelper(Context context, String name) {
        super(context, name);
    }

    public MyDevOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {

        super.onUpgrade(db, oldVersion, newVersion);
    }
}

Now can start create table and CRUD

Create table

@Entity
public class Note {

    @Id
    public Long id;

    public String text;
    
 }

Create

        DaoSession daoSession = (App.getInstance()).getDaoSession();
        NoteDao noteDao = daoSession.getNoteDao();
        
        Note note = new Note();
        note.text = "add data";
        noteDao.save(note);

Read

        DaoSession daoSession = (App.getInstance()).getDaoSession();
        NoteDao noteDao = daoSession.getNoteDao();

        List<Note> notes = noteDao.loadAll();

Update

        int id = 1;//update table id
        
        DaoSession daoSession = (App.getInstance()).getDaoSession();
        NoteDao noteDao = daoSession.getNoteDao();
        
        Note note = noteDao.queryBuilder().where(NoteDao.Properties.Id.eq(id)
        ).unique();
        note.text = "update text column";
        noteDao.save(note);

Delete

        int id = 1;//delete table id
        
        DaoSession daoSession = (App.getInstance()).getDaoSession();
        NoteDao noteDao = daoSession.getNoteDao();
        
        QueryBuilder<Note> qb = noteDao.queryBuilder();
        DeleteQuery<Note> bd = qb.where(NoteDao.Properties.Id.eq(id)).buildDelete();
        bd.executeDeleteWithoutDetachingEntities();

greendao's People

Contributors

cheekiat avatar

Stargazers

 avatar ÖZCAN YARIMDÜNYA avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.