Android-query all, Query Count, query page

Source: Internet
Author: User

1. persondao

The full persondao code is as follows:

package com.njupt.sqllist;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class PersonDao {DBOpenHelper helper ;public PersonDao(Context context){helper = new DBOpenHelper(context);}public void insert(Person p){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("insert into person(name,balance) values(?,?)", new Object[]{ p.getName(),p.getBalance()});    db.close();}public void delete(int id){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("delete from person where id = ?", new Object[]{id});db.close();}public void update(Person p){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("update person set name = ? , balance = ? where id = ? ", new Object[]{p.getName(),p.getBalance(),p.getId()});}public Person query(int id){SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.rawQuery("select name , balance from person where id = ?", new String[]{id + ""});    Person p = null ;if(c.moveToNext()){String name = c.getString(c.getColumnIndex("name"));int balance = c.getInt(1);p = new Person(id,name,balance);}return p;}public List<Person> queryAll(){SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.rawQuery("select * from person", null);List<Person> persons = new ArrayList<Person>();while(c.moveToNext()){Person p = new Person(c.getInt(0),c.getString(1),c.getInt(2));persons.add(p);}return persons;}public int queryCount(){SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.rawQuery("select count(*) from person", null); c.moveToNext();  int count = c.getInt(0); return count;}public List<Person> queryPage(int pageNum , int pageSize){SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.rawQuery("select * from person limit ?,?", new String[]{(pageNum - 1)*pageSize + "",pageSize + ""  });List<Person> persons = new ArrayList<Person>();while(c.moveToNext()){Person p = new Person(c.getInt(0),c.getString(1),c.getInt(2));persons.add(p);}return persons;}}

2. dbtest

The full test code is attached below:

Package COM. njupt. sqllist; import Java. util. list; import Java. util. random; import android. test. androidtestcase; public class dbtest extends androidtestcase {public void test1 () {dbopenhelper helper = new dbopenhelper (getcontext (); helper. getwritabledatabase ();} public void testinsert () {persondao Dao = new persondao (getcontext (); random r = new random (40000); For (INT I = 0; I <100; ++ I) {person P = new person (0, "test" + I, R. nextint (); Dao. insert (p) ;}} public void testdelete () {persondao Dao = new persondao (getcontext (); Dao. delete (1);} public void testupdate () {persondao Dao = new persondao (getcontext (); person P = new person (2, "Liu Yifei", 45000); Dao. update (p);} public void testquery () {persondao Dao = new persondao (getcontext (); person P = Dao. query (3); system. out. println (p);} public void testqueryall () {persondao Dao = new persondao (getcontext (); List <person> Persons = Dao. queryall (); For (person P: Persons) {system. out. println (p) ;}} public void testquerycount () {persondao Dao = new persondao (getcontext (); int COUNT = Dao. querycount (); system. out. println (count);} public void testquerypage () {persondao Dao = new persondao (getcontext (); List <person> Persons = Dao. querypage (2, 10); For (person P: Persons) {system. out. println (p );}}}

========================================================== ========

The paging result is as follows:

Total number of queries:

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.