The use of Greendao operation of the database, are the object or the list of objects to do the operation of the increase and deletion, the results of the operation is a list to receive!!!
1. Add a record
Stu stu01=new Stu ();
Stu01.setstuid (11);
Stu01.setstuname ("Zhang San");
Stu01.setflag (TRUE);
Studao (). Insert (STU01);
2. Add multiple Records
list<stu> list = new arraylist<> ();
Stu stu01=new Stu ();
Stu01.setstuid (one);
stu01.setstuname ("Zhang San");
Stu01.setflag (true);
List.add (stu01);
Stu stu02=new Stu ();
Stu02.setstuid (2);
stu02.setstuname ("Harry");
Stu02.setflag (true);
List.add (stu02);
Stu stu03=new Stu ();
Stu03.setstuid (long.valueof (5));
stu03.setstuname ("John Doe");
Stu03.setflag (true);
List.add (stu03);
Studao (). Insertintx (list);
3. Delete a record
(1) Delete by object
Studao (). Delete (STU01);
(2) Delete by primary key (a long type of primary key must be built in the table)
Studao (). Deletebykey (Long.valueof (5));
4. Delete multiple records (a long type of primary key is required in the table)
list<stu> list = new arraylist<> ();
Stu stu01=new Stu ();
Stu01.setid (long.valueof (1));
Stu01.setstuid (11);
Stu01.setstuname ("Zhang San");
Stu01.setflag (TRUE);
List.add (STU01);
Stu stu02=new Stu ();
Stu02.setid (long.valueof (2));
Stu02.setstuid (2);
Stu02.setstuname ("Harry");
Stu02.setflag (TRUE);
List.add (STU02);
Studao (). Deleteintx (list);
5. Delete all
Studao (). DeleteAll ();
6. Modify a record (a long type of primary key must be built in the table)
Stu stu=stu01;
Stu.setstuname ("Zhangxiaolei");
Studao (). Update (Stu);
7. Modify multiple records
list<stu> list = new arraylist<> ();
Stu stu=stu01;
Stu.setstuname ("Zhangxiaolei");
List.add (Stu);
stu=stu02;
Stu.setstuname ("recruit Son");
List.add (Stu);
Studao (). Updateintx (list);
8. Query a record
Query another field based on one field
public static String Querynamebyid (long ID) {
List<stu> Stus = Studao (). Queryraw ("Where id=?", String.valueof (ID));
Stu Stu = stus.get (0);
String name = Stu.getname ();
return name;
}
9. Querying multiple records
(1) Querying for all records when one field is multiple values
public static list<stu> Querystus () {
list<stu> stus = new arraylist<> ();
querybuilder querybuilder = Studao (). QueryBuilder ();
QueryBuilder. Whereor (StuDao.Properties.StuID.eq (2), StuDao.Properties.StuID.eq (3));
Stus = Querybuilder.list ();
return stus;
}
(2) Sort the records of the query in ascending order (descending order)
public static list<stu> Querystus () {
list<stu> stus = new arraylist<> ();
querybuilder querybuilder = Studao (). QueryBuilder ();
QueryBuilder. where (StuDao.Properties.Flag.eq (True)). ORDERASC (StuDao.Properties.StuID);
Stus = Querybuilder.list ();
return stus;
}
(3) Nested query
Public static list<stu> Querystu () {
list<stu> stus = new arraylist<> ();
querybuilder querybuilder = Studao (). QueryBuilder ();
QueryBuilder. where (StuDao.Properties.Flag.eq (True),
querybuilder.or(StuDao.Properties.StuName.eq ("Zhangxiaolei"),
StuDao.Properties.StuName.eq ("Harry" ));
Stus = Querybuilder.list ();
return stus;
}
Using Greendao to manipulate the SQLite database