Android Greendao SQLite database Operation Tool class use

Source: Internet
Author: User

The previous article describes how to build a class generation project and now describes how to use it.

The following is the Exampledaogenerator engineering code, made some changes

/* Copyright (C) Markus Junginger, Greenrobot (http://greenrobot.de) * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package De.greenrobot.daogenerator.gentest;import De.greenrobot.daogenerator.daogenerator;import De.greenrobot.daogenerator.entity;import De.greenrobot.daogenerator.property;import De.greenrobot.daogenerator.schema;import de.greenrobot.daogenerator.tomany;/** * Generates entities and DAOs for the Example Project Daoexample. * * Run it as a Java application (not Android).* * @author Markus */public class Exampledaogenerator {public static void main (string[] args) throws Exception {/        /The first parameter of the method is used to update the database version number, and the second parameter is the package path of the DAO class to be generated.        Schema schema = new schema (1, "de.greenrobot.daoexample");        And then to build the table Addnote (schema);        Addcustomerorder (schema); Set the project path of the target project for which you want to generate DAO files, where src-gen this directory name needs to be created manually before it is run, or the new Daogenerator () will be error. Generateall (schema, ". /..    /helloworld/src-gen ");      } private static void Addnote (schema Schema) {/** * When using Greendao, an entity class can only correspond to one table, so it is not possible to do one table for multiple entity classes, or multiple tables to share an object type.     * Subsequent upgrades will not be extended for this point. * */Entity note = schema.addentity ("note");//CREATE Table//note.addidproperty ();//Add ID column note.addidproperty () . PrimaryKey (). AutoIncrement ();//Set a self-Growth ID column as the primary key: Note.addstringproperty ("text"). Notnull ();//create non-empty columns Note.addstr    Ingproperty ("comment"). unique ();//Create a unique//note.adddateproperty ("date"); } private static void Addcustomerorder (schema schema) {Entity custOmer = Schema.addentity ("Customer");        Customer.addidproperty ();        Customer.addstringproperty ("name"). Notnull ();        Entity order = schema.addentity ("Order"); Order.settablename ("ORDERS");        "ORDER" is a reserved keyword order.addidproperty ();        Property OrderDate = Order.adddateproperty ("date"). GetProperty ();        Property CustomerId = Order.addlongproperty ("CustomerId"). Notnull (). GetProperty ();        Order.addtoone (Customer, customerId);        Tomany customertoorders = Customer.addtomany (order, customerId);        Customertoorders.setname ("Orders");    CUSTOMERTOORDERS.ORDERASC (orderDate); }}

The above code has two main

Schema schema = new schema (1, "de.greenrobot.daoexample");

The first parameter is the version number, and the second parameter is the resulting class placed in the reference project, which needs to be combined with the following code

New Daogenerator (). Generateall (Schema, ".. /.. /Helloworld/src-gen ");

Then the resulting file will be placed in

exampledaogenerator helloworld/src-gen/de/greenrobot/daoexample in the two-level directory of the project catalog directory.

1, we in exampledaogenerator project, press the ctrl+f11 of the keyboard will automatically generate four Java classes put to helloworld/src-gen/de/Greenrobot/ Under the daoexample directory


2, put together the related classes of the 4 class dependencies, the dependent class directly put the contents of the "Greendao-master\daocore\src\de" directory together with 4 classes.

3. Set the Src-gen directory to the compiled folder

4, According to the generated class to increase the relevant code, here only to paste the relevant code

    Private Daomaster Daomaster;    Private Daosession daosession;    Private Notedao Notedao;    private cursor cursor;    Private sqlitedatabase db; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);   Devopenhelper helper = new Daomaster.devopenhelper (mainactivity.this, "Note", null);        db = Helper.getwritabledatabase ();        Daomaster = new Daomaster (db);        Daosession = Daomaster.newsession ();        Notedao = Daosession.getnotedao ();}

Final Button btn6 = (Button) Findviewbyid (R.ID.BUTTON6); Btn6.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {        toast.maketext (mainactivity.this, "Length:" + notedao.count (), Toast.length_short). Show ();        Note Note = new Note (NULL, "NoteText1", "comment1"); try {    Notedao.insert (note),} catch (Exception e) {        toast.maketext (mainactivity.this, "Data repeats:" + Notedao.count () , Toast.length_short). Show ();}    

5, according to Ctrl+shift+o Auto-completion related categories

6, download apk to mobile phone, you can.


Add:

During debugging, after modifying the class that generated the project, it is found that the code for recompiling the HelloWorld project has a code exception in "db = Helper.getwritabledatabase ();"

This issue is not known for some reason. But there is a way to solve it: on the phone----in the Settings screen--Select the corresponding application (for me, it is HelloWorld this app)--Erase the data

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.