Simple configuration and use of Android Greendao

Source: Internet
Author: User
Tags sqlite database

Recently self-taught to do something to use a collection of functions, and then I want to store things in SQLite, but the value of their own when the use of the entity class, so save up is also more trouble, so from the Internet to find a Greendao open source framework is very fire, not only high efficiency, and memory also occupies a small , very convenient.

Here I briefly introduce how to configure, as for the other increase, delete, change, check, online are many, need to use the small partners to check it!!

First we have to understand that Greendao is a quick generator of Java code templates that encapsulates the various operations of our entity classes. So

1, we will use Eclipse or MyEclipse to build a Java project, we need to build a Lib directory under our project to put our two packages (no small partners can go online download)

2. Right-click our project and select Properties from the popup menu to add our two jar packages to our project

3, the operation is completed, we can write code, set up a class, in the inside write down the following code

 PackageCom.fanlei.greenDao;;ImportDe.greenrobot.daogenerator.DaoGenerator;Importde.greenrobot.daogenerator.Entity;ImportDe.greenrobot.daogenerator.Schema; Public classExampledaogenerator { Public Static voidMain (string[] args)throwsIOException, Exception {schema schema=NewSchema (1, "De.greenrobot.daoexample");            Addnote (schema); NewDaogenerator (). Generateall (Schema, "E:\\daoexample\\src-gen")); }        Private Static voidaddnote (schema Schema) {Entity News= Schema.addentity ("News"); News.addstringproperty ("Author"); News.addstringproperty ("Content"); News.addstringproperty ("id"); News.addstringproperty ("IMG"); News.addstringproperty ("Pagetag"); News.addstringproperty ("Time"); News.addstringproperty ("Title"); }  }

First of all

Schema schema = new schema (1, "de.greenrobot.daoexample"); It is necessary to have an entity class that needs to be built,

Two parameters The first is a version, that is, our SQLite database version, later if the need for a database upgrade, this parameter is required.

The second is a path, which is where the generated. java files are placed after you run the code.

Method Addnote () is used to add the entity class, you want to generate the entity class inside what kind of attribute, this need to join, the type of property is also very comprehensive

Entity news = Schema.addentity ("News");

Generates a file and a file, and the file encapsulates the operation of the database.

New Daogenerator (). Generateall (Schema, "E:\\daoexample\\src-gen");

The second parameter is where you want the generated files to be stored. This path requires the user to build it beforehand, otherwise it will report an exception.

The last generated file is in addition to the two necessary. java files, and there is DAO for your entity class and entity class.

4, it is best to take these several. java files into your Android project can be used, Android also need a jar package to support, do not forget to import into. (no online download available)

5, in order to prevent us always to create a session, the official recommended that we put the session generation into the application, and then we can use the Get method to obtain

1  Public classMyApplicationextendsApplication {2 3     PrivateDaomaster.devopenhelper Helper;4     PrivateSqlitedatabase db;5     PrivateDaomaster Daomaster;6     Privatedaosession daosession;7 8 @Override9      Public voidonCreate () {Ten  One  AHelper =NewDaomaster.devopenhelper ( This, "News_info-db",NULL); -db =helper.getwritabledatabase (); -Daomaster =Newdaomaster (db); theDaosession =daomaster.newsession (); -  } -}

6, insert operation is also very simple

 private   news news;     private   Newsdao Newsdao; @Override  protected  void   OnCreate (Bundle savedinstancestate) { super  .oncreate (savedinstancestate);        Setcontentview (R.layout.activity_news_info);        Getsupportactionbar (). Hide ();  Newsdao  = Myapplication.getmyapplocation (). Getdaosession (). Getnewsdao (); //  get this tool DAO     Newsdao.insert (news);      //  insert INTO database  

If you insert it for the first time, a table table name is the name of the class, and the field name is the name of the property, and the table is specifically for the news entity class.

7, other operations please search by yourself, or look at the official documents

Simple configuration and use of Android Greendao

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: 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.