Android Greendao Use (i) to create a database __ database

Source: Internet
Author: User
Tags generator

The use of Greendao has been a long time, unknowingly has been updated to the 3.x version.

Note The basic methods used:

1. First in the app directory under the main folder under the new Java-gen directory:

Note: A Java file will be specified in this directory, of course this is not necessary, you can customize the directory, so the purpose is to clear the directory structure and so on.

2. In the app under the Build.gradle designated Identification Java-gen, by the way pour greendao dependence:

Compile "org.greenrobot:greendao:3.2.2"

Sourcesets {
        main {
            java.srcdirs = [' Src/main/java ', ' Src/main/java-gen ']
        }
    }
3. New Java Project:


Then pour dependencies into Java engineering:

Compile ' org.greenrobot:greendao-generator:3.2.2 '


4. Create a new Java class in Java Engineering: Greendaogenerator, where I created a new table to run this Java class,

Greendao will generate the required classes directly into the directory we have just specified, based on our settings and the newly created fields in our table:

package com.example;
Import Org.greenrobot.greendao.generator.DaoGenerator;
Import org.greenrobot.greendao.generator.Entity;

Import Org.greenrobot.greendao.generator.Schema; /** * Java Engineering Build database table */public class Greendaogenerator {public static void main (string[] args) {//Generate database entity classes
        Xxentity corresponds to the table schema schema of the database = new schema (1, "com.school.entity");
        Addtestgroup (schema);

        Schema.setdefaultjavapackagedao ("Com.school.dao"); try {new Daogenerator (). Generateall (Schema, "/users/katsurayasubumi/studioprojects/reliable_testinput/app/src
        /main/java-gen ");
        catch (Exception e) {e.printstacktrace (); }/** * Test Project Grouping table */private static void Addtestgroup (schema schema) {Entity Entity = Schem
        A.addentity ("Testgroup");
        Entity.addidproperty ();
        Entity.addintproperty ("group");
    Entity.addstringproperty ("name"); }


}
Note: 1. One of the Entity.addidproperty () is to give this table a new primary key, from the growth ID. You can also replace the "id" that is uniquely identified in your actual requirements.

In the 2.main function, schema is instantiated, the first parameter is the database version number, and the second parameter is the directory that generates the corresponding table entity class. Below Setdefal. Is the directory of the class that the specified Greendao generates.


The above diagram is the Java project, the corresponding directory generated the corresponding files.


5. The next step is to encapsulate the database operations class.

public class Daomanager {private static final String TAG = DaoManager.class.getSimpleName ();

    Database name private static final String db_name = "Reliable.sqlite";

    Multithreading access private volatile static Daomanager manager;

    private static Daomaster.devopenhelper helper;

    private static Daomaster Daomaster;

    private static daosession daosession;

    private context;
        /** * Use a single case mode to obtain an object to manipulate the database/public static Daomanager getinstance () {Daomanager instance = null;
                    if (manager = = null) {synchronized (Daomanager.class) {if (instance = null) {
                    Instance = new Daomanager ();
                Manager = instance;
    }} return manager;
    "public void init" {this.context = context; 
/** * To determine if there is a database * If not then create the database/public Daomaster Getdaomaster () {if (Daomaster = = null) { //           Daomaster.devopenhelper helper = new Daomaster.devopenhelper (context, db_name, null);
            Myopenhelper myopenhelper = new Myopenhelper (context, db_name, null);
        Daomaster = new Daomaster (Myopenhelper.getwritabledatabase ());
    return daomaster; /** * Complete the operation of the database add delete modify query, etc. * NOTE: Only an interface/public daosession getdaosession () {if (Daosessi
            on = = null) {if (Daomaster = null) {Daomaster = Getdaomaster ();
        } daosession = Daomaster.newsession ();
    return daosession;
        /** * Open Output log operation * Note: The default is closed */public void Setdebug () {querybuilder.log_sql = true;
    Querybuilder.log_values = true;
        /** * Close all operations * Note: After the database is opened, use must be closed/public void CloseConnection () {closehelper ();
    Closedaosession (); public void Closehelper () {if (helper!= null) {HeLper.close ();
        Helper = null;
            } public void Closedaosession () {if (daosession!= null) {daosession.clear ();
        Daosession = null; }

    }

}



6. The final is the specific operation of the table, the next to record:













Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.