Using the crud method that comes with SQLite to operate the database is not in favor of use, because the efficiency is not high to directly operate the SQL statement
So I only paste the example
Otherpersonservice
Package Com. tjp. Service;
Import Java. util. arraylist;
Import Java. util. List;
Import Android. content. contentvalues;
Import Android. content. context;
Import Android. database. cursor;
Import Android. database. SQLite. sqlitedatabase;
Import Com. tjp. model. person;
Public Class Otherpersonservice {
Private Dbopenhelp openhelp = Null ;
Private Context context;
Public Otherpersonservice (context ){
This . Context = Context;
Openhelp = New Dbopenhelp (context );
}
Public Void Save (person ){
Sqlitedatabase = Openhelp. getwritabledatabase (); // The data needs to be changed. This method enables the database to be read and written with the cache function.
// Database.exe csql ("insert into person (name) values (?) ", New object [] {person. getname ()});
Contentvalues values = New Contentvalues ();
Values. Put ( " Name " , Person. getname ());
Database. insert ( " Person " , Null , Values ); // Whether there is any data, you want to save the data in the database,
// Database. insert ("person", "name", null ); // If no third field is specified or the number of the third parameter is 0, you must specify a null field.
}
Public Void Update (person ){
Sqlitedatabase = Openhelp. getwritabledatabase ();
// Database.exe csql ("Update person set name =? Where personid =? ", New object [] {person. getname (), person. getpersonid ()});
Contentvalues values = New Contentvalues ();
Values. Put ( " Name " , Person. getname ());
Database. Update ( " Person " , Values, " Personid =? " , New String [] {string. valueof (person. getpersonid ())});
}
Public Void Delete ( Int Personid ){
Sqlitedatabase = Openhelp. getwritabledatabase ();
// Database.exe csql ("delete from person where personid =? ", New object [] {personid });
Database. Delete ( " Person " , " Personid =? " , New String [] {string. valueof (personid )});
}
Public Person find ( Int Personid ){
Sqlitedatabase = Openhelp. getwritabledatabase ();
Person = Null ;
// Cursor cursor = database. rawquery ("select * From person where personid =? ", New string [] {string. valueof (personid). tostring ()});
Cursor cursor = Database. Query ( " Person " , New String [] { " Personid " , " Name " },
" Personid =? " , New String [] {string. valueof (personid )}, Null , Null , Null );
If (Cursor. movetofirst ()){ // If the movement is successful, it indicates the operation exists.
Int Personida = Cursor. getint (cursor. getcolumnindex ( " Personid " ));
String name = Cursor. getstring (cursor. getcolumnindex ( " Name " ));
Person = New Person (personida, name );
}
Cursor. Close ();
Return Person;
}
Public List < Person > Getscrolldate ( Int Offerset, Int Maxresult ){
List < Person > Persons = New Arraylist < Person > ();
Sqlitedatabase = Openhelp. getwritabledatabase ();
// String SQL = "select * From person limit ?,? ";
// Cursor cursor = database. rawquery (SQL, new string [] {string. valueof (offerset), String. valueof (maxresult )});
Cursor cursor = Database. Query ( " Person " , Null , Null , Null , Null , Null , Null , Offerset + " , " + Maxresult );
While (Cursor. movetonext ()){
Int Personida = Cursor. getint (cursor. getcolumnindex ( " Personid " ));
String name = Cursor. getstring (cursor. getcolumnindex ( " Name " ));
Person = New Person (personida, name );
Persons. Add (person );
}
Return Persons;
}
Public Long Getcount (){
Sqlitedatabase = Openhelp. getwritabledatabase ();
// Cursor cursor = database. rawquery ("select count (*) from person", null );
Cursor cursor = Database. Query ( " Person " , New String [] { " Count (*) " }, Null , Null , Null , Null , Null );
Cursor. movetofirst ();
Int Count = Cursor. getint ( 0 );
Cursor. Close ();
Return Count;
}
}
When the database is updated, the onupgrade command is executed as long as the version number is updated.