For example if you want create a database with two Customer,Order tables then you need to follow below steps to create a database with Customer and Order tables.
-> Create ExampleDaoGenerator.java class in SampleJava Project.This class contains the below code.
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 {
Schema schema = new Schema(3, "com.android.example"); //your package name
createDB(schema);
new DaoGenerator().generateAll(schema, "../DaoExampleGenerator/src-gen"); //where you want to store the generated classes.
}
private void createDB(Schema schema) {
//Customer table...
Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addIntProperty("vvk");
customer.addStringProperty("name").notNull();
//Order table
Entity order = schema.addEntity("Order");
order.setTableName("ORDERS");
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);
}
}
-> Add greenDAO-generator.jar file to your java project.
-> Then Run the above java project.You will get the below classes in src-gen\com\android\dharani folder.
. CustomerDao.java
. Customer.java
. DaoMaster.java
. DaoSession.java
-> Then create sampleAndroid project and add greenDAO.jar file to your project.
-> Copy the above generated classes to your Android project.
-> Then use the below code to do different types of database operations.
Creating Database:-
DevOpenHelper ex_database_helper_obj = new DaoMaster.DevOpenHelper(SharedVariables.globalContext, "Example_DB.sqlite", null);
SQLiteDatabase ex_db= ex_database_helper_obj.getWritableDatabase();
DaoMaster.createAllTables(ex_db, true);
ex_db.close();
ex_database_helper_obj.close();
For doing any type operation(insert,delete,update) on database you must use the below code.
DevOpenHelper ex_database_helper_obj = new DaoMaster.DevOpenHelper(SharedVariables.globalContext, "Example_DB.sqlite", null);
SQLiteDatabase ex_db= ex_database_helper_obj.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster( ex_db);
DaoSession daoSession = daoMaster.newSession();
clsCustomerDao customerDao =daoSession.getClsCustomerDao();
customerDao.insert(customerclassObject); //insert...
customerDao.delete(customerclassObject); //delete...
customerDao.update(customerclassObject); //update...
customerArrayList=customerDao.queryBuilder().where(Properties.Id.eq(1)).list(); //Query...
daoSession.clear();
ex_db.close();
ex_database_helper_obj.close();
Please Click the below links for above mentioned jar files.
link for greenDAO-generator.jar :- http://www.4shared.com/file/ dMD0rbxI/greenDAO-generator. html
link for greenDAO.jar:- http://www.4shared.com/file/V- 6jdz8E/greenDAO.html
-> Create ExampleDaoGenerator.java class in SampleJava Project.This class contains the below code.
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 {
Schema schema = new Schema(3, "com.android.example"); //your package name
createDB(schema);
new DaoGenerator().generateAll(schema, "../DaoExampleGenerator/src-gen"); //where you want to store the generated classes.
}
private void createDB(Schema schema) {
//Customer table...
Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addIntProperty("vvk");
customer.addStringProperty("name").notNull();
//Order table
Entity order = schema.addEntity("Order");
order.setTableName("ORDERS");
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);
}
}
-> Add greenDAO-generator.jar file to your java project.
-> Then Run the above java project.You will get the below classes in src-gen\com\android\dharani folder.
. CustomerDao.java
. Customer.java
. DaoMaster.java
. DaoSession.java
-> Then create sampleAndroid project and add greenDAO.jar file to your project.
-> Copy the above generated classes to your Android project.
-> Then use the below code to do different types of database operations.
Creating Database:-
DevOpenHelper ex_database_helper_obj = new DaoMaster.DevOpenHelper(SharedVariables.globalContext, "Example_DB.sqlite", null);
SQLiteDatabase ex_db= ex_database_helper_obj.getWritableDatabase();
DaoMaster.createAllTables(ex_db, true);
ex_db.close();
ex_database_helper_obj.close();
For doing any type operation(insert,delete,update) on database you must use the below code.
DevOpenHelper ex_database_helper_obj = new DaoMaster.DevOpenHelper(SharedVariables.globalContext, "Example_DB.sqlite", null);
SQLiteDatabase ex_db= ex_database_helper_obj.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster( ex_db);
DaoSession daoSession = daoMaster.newSession();
clsCustomerDao customerDao =daoSession.getClsCustomerDao();
customerDao.insert(customerclassObject); //insert...
customerDao.delete(customerclassObject); //delete...
customerDao.update(customerclassObject); //update...
customerArrayList=customerDao.queryBuilder().where(Properties.Id.eq(1)).list(); //Query...
daoSession.clear();
ex_db.close();
ex_database_helper_obj.close();
Please Click the below links for above mentioned jar files.
link for greenDAO-generator.jar :- http://www.4shared.com/file/
link for greenDAO.jar:- http://www.4shared.com/file/V-
Exception in thread "main" java.lang.NoClassDefFoundError: freemarker/template/ObjectWrapper
ReplyDeleteat de.greenrobot.daogenerator.gentest.ExampleDaoGenerator.main(ExampleDaoGenerator.java:17)
Caused by: java.lang.ClassNotFoundException: freemarker.template.ObjectWrapper
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
just add a valid freemarker.jar in your project classpath
DeleteHi Joseph, greendao uses freemaker java template engine gradle automatically downloads it from repository. download it from here
ReplyDeletehttp://freemarker.org/freemarkerdownload.html
How to deal with already created database in SQLite rather than using DaoGenerator..
ReplyDelete