Http://www.hiapk.com/bbs/thread-57703-1-1.html
Use android
The sqliteopenhelper In the SDK can be used to conveniently operate the SQLite database.
Platform limitations: SQLite on mobile phones cannot perform very complex select operations, but the general addition, deletion, modification, and query Functions
It is also quite complete, enough to meet the needs of mobile platforms.
First, we need to inherit the sqliteopenhelper class and overwrite two abstract methods.
Public class
Testdatabase extends
Sqliteopenhelper {
Public
Testdatabase (context ){
// Create a database named test_db
Super
(Context, "test_db"
, Null
, 1 );
}
@ Override
Public void
Oncreate (sqlitedatabase dB ){
// If the table does not exist during execution, create it. Note that the SQLite database must have a _ id field as the primary key; otherwise, an error is returned during query.
String SQL = "CREATE TABLE mytable (_ id integer primary key autoincrement, stext text )"
;
Db.exe csql (SQL );
}
@ Override
Public void
Onupgrade (sqlitedatabase dB, int
Oldversion, int
Newversion ){
// Delete the original table when the database is changed
And create a new table.
String SQL = "Drop table if exists mytable"
;
Db.exe csql (SQL );
Oncreate (db );
}
}
The method for adding, deleting, modifying, and querying
Public
Cursor select (){
Sqlitedatabase DB = getreadabledatabase ();
Cursor cur = dB. Query ("mytable"
, Null
, Null
, Null
, Null
, Null
, Null
);
Return
Cur;
}
Public long
Insert (string text ){
Sqlitedatabase DB = getwritabledatabase ();
Contentvalues CV = new
Contentvalues ();
Cv. Put ("stext"
, Text );
Long ROW = dB. insert ("mytable"
, Null
, CV );
Return
Row;
}
Public int
Delete (int
ID ){
Sqlitedatabase DB = getwritabledatabase ();
String where = "_ id =? "
;
String [] wherevalue = {INTEGER. tostring (ID )};
Return
DB. Delete ("mytable"
, Where, wherevalue );
}
Public int
Update (int
ID, string text ){
Sqlitedatabase DB = getwritabledatabase ();
String where = "_ id =? "
;
String [] wherevalue = {INTEGER. tostring (ID )};
Contentvalues CV = new
Contentvalues ();
Cv. Put ("stext"
, Text );
Return
DB. Update ("mytable"
, CV, where, wherevalue );
}
The db. query method parameters are complex. Here All null is set to save time. The specific parameters are as follows:
Public
Cursor query (string table, string []
Columns, string selection, string [] selectionargs, stringgroupby, string
Having, string orderby)
Table: Table name. It cannot be null.
Columns: array of column names to be returned. If this parameter is set to null, all columns are returned.
Selection: WHERE clause. If the WHERE clause is not required, set it to null, as shown in "_ id =? ", The parameter to be entered here is ?, Fill the selectionargs below
Selectionargs: the value required by the WHERE clause. This array is filled with each question mark in selection.
Groupby: groupby clause
Having: Having clause
Orderby: Order by clause
Well, it's so easy to operate SQLite. Of course, at the same time, we need to create an interface to display data, so this article will not talk about it any more.