Using Greendao to manipulate the SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database

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

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.