The previous article describes how to create a class generation project and how to use it.
The following is the ExampleDaoGenerator project code and some modifications have been made.
/** Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de) ** Licensed under the Apache License, Version 2.0 (the "License "); * you may not use this file before t in compliance with the License. * You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software * distributed under the Licen Se is distributed on an "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. toates;/*** 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 this method is used to update the database version number, the second parameter is the package path of the DAO class to be generated. Schema schema = new Schema (1, "de. greenrobot. daoexample "); // create the table addNote (schema); // addCustomerOrder (schema); // set the project path of the target project to generate the DAO file, the directory name src-gen needs to be manually created before running; otherwise, the new DaoGenerator () error is returned (). generateAll (schema ,".. /.. /HelloWorld/src-gen ");} private static void addNote (Schema schema) {/*** when greenDAO is used, one entity class can only correspond to one table, currently, one table cannot correspond to multiple entity classes, or multiple tables share one object type. * Subsequent upgrades will not be used for this purpose. **/Entity note = schema. addEntity ("Note"); // create a table // note. addIdProperty (); // Add the ID column note. addIdProperty (). primaryKey (). autoincrement (); // set an auto-increment ID column as the primary key: note. addStringProperty ("text "). notNull (); // create a non-empty column note. addStringProperty ("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); tow.mermertoorders = customer. addtoorders (order, customerId); customerToOrders. setName ("orders"); customerToOrders. orderAsc (orderDate );}}
The above code mainly has two
Schema schema = new Schema (1, "de. greenrobot. daoexample ");
The first parameter is the version number, and the second parameter is placed at the place where the generated class references the project.
New DaoGenerator (). generateAll (schema, ".../../HelloWorld/src-gen ");
Then the generated files will be placed in
The HelloWorld/src-gen/de/greenrobot/daoexample directory in the preceding two directories of the ExampleDaoGenerator project directory.
1. In the ExampleDaoGenerator project, press ctrl + F11 on the keyboard to automatically generate four java classes under the HelloWorld/src-gen/de/greenrobot/daoexample directory.
2. Put these four dependent classes together. The dependent classes directly put the content in the "greenDAO-master \ DaoCore \ src \ de" directory together with the four classes.
3. Set the src-gen directory as a compilation folder.
4. Add the relevant code according to the generated class. Here, only the code used is pasted.
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, "duplicate data:" + noteDao. count (), Toast. LENGTH_SHORT ). show ();}
5. Press ctrl + shift + o to automatically complete related classes.
6. Download the APK to your mobile phone.
Supplement:
During the debugging process, after modifying the class of the generated project, we found that the Code exception occurred when re-compiling the helloworld project. The code is located in "db = helper. getWritableDatabase ();"
I do not know the cause of this problem. But there is a solution: on the mobile phone end --> On the settings page --> select the corresponding application (for me, it is the helloworld application) --> clear data