Use of Android SQLiteOpenHelper CRUD

Source: Internet
Author: User

Copy codeThe Code is as follows: public class DBOpenHelper extends SQLiteOpenHelper {
// The class is not instantiated and cannot be used as a parameter of the parent class constructor. It must be declared as static
Public DBOpenHelper (Context context, int version ){
Super (context, "SQLite. db", null, version );
// The third parameter CursorFactory specifies the factory class for obtaining a cursor instance during query execution. Setting null indicates that the default cursor factory is used;
}
@ Override
Public void onCreate (SQLiteDatabase db) {// called when the database was first created. The version number assigned when the database was first created is 0.
// SQLiteDatabase is used to generate database tables. database storage path: <package name>/databases/
Db.exe cSQL ("create table person (personid integer primary key autoincrement, name varchar (20 ))");
}
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// called when the version number is upgraded; it is not called if the database does not exist
Db.exe cSQL ("alter table person add phone varchar (12) null"); // add a column to the table
}
}
DBOpenHelper dbOpenHelper = new DBOpenHelper (getContext (), 1 );
DbOpenHelper. getWritableDatabase (); // create database table or open database table

Public class PersonService {
PrivateDBOpenHelperDbOpenHelper;
Public PersonService (Context context ){
Super ();
This. dbOpenHelper = new DBOpenHelper (context, 2 );
}
Public void save (Person person ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
// SQLiteDatabase db2 = dbOpenHelper. getWritableDatabase (); With the cache function, the same Helper uses this method to get the object db;
Db.exe cSQL ("insert into person (name, phone) values (?,?) ", New Object [] {person. getName (), person. getPhone ()});
}
Public void delete (Integer id ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
Db.exe cSQL ("delete from person where personid =? ", New Object [] {id });
}
Public void update (Person person ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
Db.exe cSQL ("update person set name = ?, Phone =? Where personid =? ", New Object [] {person. getName (), person. getPhone (), person. getId ()});
}
Public Person find (Integer id ){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
// If the database disk space is not full, the obtained instance is the object obtained by getWritableDatabase (), because this method calls the getWritableDatabase method;
Cursor cursor = db. rawQuery ("select * from person where personid =? ", New String [] {id. toString ()});
If (cursor. moveToFirst ()){
Int personid = cursor. getInt (cursor. getColumnIndex ("personid "));
String name = cursor. getString (cursor. getColumnIndex ("name "));
String phone = cursor. getString (cursor. getColumnIndex ("phone "));
Return new Person (personid, name, phone );
}
Cursor. close ();
Return null;
}
/*
* Retrieve paging data
*/
Public List getScollData (int offest, int maxResult ){
List persons = new ArrayList ();
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Cursor cursor = db. rawQuery ("select * from person order by personid asc limit ?,? ", New String [] {String. valueOf (offest), String. valueOf (maxResult )});
While (cursor. moveToNext ()){
Int personid = cursor. getInt (cursor. getColumnIndex ("personid "));
String name = cursor. getString (cursor. getColumnIndex ("name "));
String phone = cursor. getString (cursor. getColumnIndex ("phone "));
Persons. add (new Person (personid, name, phone ));
}
Cursor. close ();
Return persons;
}
Public long getCount (){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Cursor cursor = db. rawQuery ("select count (*) from person", null );
Cursor. moveToFirst ();
Long result = cursor. getLong (0 );
Return result;
}
}

Public class OtherPersonService {
PrivateDBOpenHelperDbOpenHelper;
Public OtherPersonService (Context context ){
Super ();
This. dbOpenHelper = new DBOpenHelper (context, 2 );
}
Public void save (Person person ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
ContentValuesValues = new ContentValues (); // ContentValues is used to save the value of a field.
Values. put ("name", person. getName ());
Values. put ("phone", person. getPhone ());
Db. insert ("person", null, values); // The third parameter is the field value, and the second parameter is the null field. If the third parameter is null
}
Public void delete (Integer id ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
Db. delete ("person", "personid =? ", New String [] {id. toString ()});
}
Public void update (Person person ){
SQLiteDatabase db = dbOpenHelper. getWritableDatabase ();
ContentValues values = new ContentValues ();
Values. put ("name", person. getName ());
Values. put ("phone", person. getPhone ());
Db. update ("person", values, "personid =? ", New String [] {person. getId (). toString ()});
}
Public Person find (Integer id ){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Cursor cursor = db. query ("person", null, "personid =? ", New String [] {id. toString ()}, null );
If (cursor. moveToFirst ()){
Int personid = cursor. getInt (cursor. getColumnIndex ("personid "));
String name = cursor. getString (cursor. getColumnIndex ("name "));
String phone = cursor. getString (cursor. getColumnIndex ("phone "));
Return new Person (personid, name, phone );
}
Cursor. close ();
Return null;
}
/*
* Retrieve paging data
*/
Public List getScollData (int offest, int maxResult ){
List persons = new ArrayList ();
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Cursor cursor = db. query ("person", null, "personid asc", offest + "," + maxResult );
// Db. query (table, columns, selection, selectionArgs, groupBy, having, orderBy, limit );
While (cursor. moveToNext ()){
Int personid = cursor. getInt (cursor. getColumnIndex ("personid "));
String name = cursor. getString (cursor. getColumnIndex ("name "));
String phone = cursor. getString (cursor. getColumnIndex ("phone "));
Persons. add (new Person (personid, name, phone ));
}
Cursor. close ();
Return persons;
}
Public long getCount (){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Cursor cursor = db. query ("person", new String [] {"count (*)"}, null, null );
// Db. query (table, columns, selection, selectionArgs, groupBy, having, orderBy );
Cursor. moveToFirst ();
Long result = cursor. getLong (0 );
Return result;
}
}

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.