SqLite Frame Greendao

Source: Internet
Author: User
Tags sesion sqlite

Greendao:
Generates a data access without the code we write to access the database:

CORE schematic diagram

Generate code
is to generate a build factory of the corresponding Java class with the generator.

 Public   static void main(string[] args)
{
//ID name age sex
//Schema S=new schema (version number, code package name);
Schema s =NewSchema (1,"Com.itheima.redbaby.dao");
//Description Object
Entity person = s.addentity ("Person");
Person.addidproperty (). AutoIncrement (). PrimaryKey ();//ID
Person.addstringproperty ("Name");
Person.addintproperty ("Age");
Person.addstringproperty ("Sex");
//Generate code
//New Daogenerator (). Generateall (S, input path);
Try{//: Working space
NewDaogenerator (). Generateall (s),".. /greendao_redbaby/src/");
}Catch(IOException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}Catch(Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}

}
Code generation in the specified path is as follows

Test code
@Test
Public void testLoad() {
Daosession sesion = getsession ();
Persondao person = Sesion.getpersondao ();
Person P=person.load (1L);
LOG.I ("WZX", P.getname ());
}

@Test
Public void testloadall() {
Daosession sesion = getsession ();
Persondao person = Sesion.getpersondao ();
list<person> list = Person.loadall ();
for(Person P:list) {
LOG.I ("WZX", P.getname ());
}

}

@Test
Public void testinsert() {

Daosession sesion = getsession ();
//Get specific one DAO Perpsondao
Persondao DAO = Sesion.getpersondao ();
Person p =NewPerson ();
P.setage (1);
P.setname ("The song of Zhang San.");
P.setsex ("Man");
Dao.insert (P);
}

Public daosession getsession() {
//Description Data shopping.db
//Sqliteopenhelper
//|--devopenhelper
Sqlitedatabase db =NewDaomaster.devopenhelper (Mcontext,"Shopping.db",NULL). Getwritabledatabase ();
Daomaster DM =NewDaomaster (DB);
//Get collection of DAO daosession Persondao
Daosession sesion = Dm.newsession ();
returnsesion;
}

@Test
Public void testdeletebykey() {
Daosession sesion = getsession ();
Persondao Persondao = Sesion.getpersondao ();
Persondao.deletebykey (1L);
}
@Test
Public void testupdate() {
Daosession sesion = getsession ();
Persondao Persondao = Sesion.getpersondao ();
Person P=persondao.load (1L);
P.setage ( A);
P.setsex ("Woman");
Persondao.update (P);

}

Summary :
The overall simple use of our database, so we are too much to write SQL code;
Just need us to put each row of the database. It is possible to have a javabean field.
Simple and rude;

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