Based on the Android sqliteopenhelper && CRUD usage _android

Source: Internet
Author: User

Copy Code code as follows:

public class DBOPenhelper extends Sqliteopenhelper {
Class is not instantiated and cannot be used as a parameter to a parent class constructor and must be declared static
Public Dbopenhelper (context, int version) {
Super (context, "sqlite.db", null, version);
The third parameter cursorfactory specifies the factory class that obtains a cursor instance when executing the query, set to NULL to represent the default cursor factory using the system;
}
@Override
public void onCreate(Sqlitedatabase db) {//The database was invoked the first time it was created, and the version number assigned at the beginning of the creation of the database is 0
Sqlitedatabase used to generate database tables; database storage path: <package name>/databases/
Db.execsql ("CREATE TABLE Person" (PersonID integer primary key autoincrement, name varchar (20));
}
@Override
public void Onupgrade(sqlitedatabase db, int oldversion, int newversion) {///when the version number is upgraded and not called if the database does not exist
Db.execsql ("ALTER TABLE person add phone varchar () NULL")//Add a column to the table
}
}
dbopenhelper dbopenhelper = new Dbopenhelper (GetContext (), 1);
Dbopenhelper.getwritabledatabase ()//will create database table Or/open database table

public classPersonservice{
PrivateDbopenhelperDbopenhelper;
Public Personservice {
Super ();
This.dbopenhelper = new Dbopenhelper (context, 2);
}
public void Save (person person) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Sqlitedatabase DB2 = Dbopenhelper.getwritabledatabase (); There is a caching function, the same helper uses this method to obtain the object is DB;
Db.execsql ("INSERT into person (Name,phone) VALUES (?,?)", new object[] {person.getname (), Person.getphone ()});
}
public void Delete (Integer id) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Db.execsql ("Delete from person where personid=?", new object[] {ID});
}
public void update (person person) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Db.execsql ("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 resulting instance isgetwritabledatabase ()The resulting object; Because this method invokes 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 to new person (PersonID, name, phone);
}
Cursor.close ();
return null;
}
/*
* Get the 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), S Tring.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 classOtherpersonservice{
PrivateDbopenhelperDbopenhelper;
Public Otherpersonservice {
Super ();
This.dbopenhelper = new Dbopenhelper (context, 2);
}
public void Save (person person) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
contentvaluesvalues = new Contentvalues ();//Contentvalues value specifically for saving fields
Values.put ("Name", Person.getname ());
Values.put ("Phone", Person.getphone ());
Db.insert ("person", null, values); The third parameter is a field value, the second parameter is a null field, if the third argument 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, NULL, 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 to new person (PersonID, name, phone);
}
Cursor.close ();
return null;
}
/*
* Get the paging data
*/
Public List getscolldata (int offest, int maxresult) {
List persons = new ArrayList ();
Sqlitedatabase db = Dbopenhelper.getreadabledatabase ();
Cursor Cursor = db.query ("person", NULL, NULL, NULL, NULL, NULL, "PersonID ASC", Offest + "," + Maxresult);
Db.query (table, columns, selection, Selectionargs, GroupBy, having, and 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, NULL, NULL, NULL);
Db.query (table, columns, selection, Selectionargs, GroupBy, having, by);
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.