Android provides APIs for creating and using SQLite databases (application programming Interface, Application programming Interface).
In the Android system, the SQLite database is managed and manipulated mainly by the class Sqlitedatabase and Sqliteopenhelper.
(i) Sqliteopenhelper class
Sqliteopenhelper is a helper class for sqlitedatabase that manages the creation of databases and the updating of versions.
Because it is an abstract class, you must create a class to inherit it and implement two abstract methods, OnCreate () and the Onupgrade () method.
construction method
method |
description |
< P>sqliteopenhelper (context context, String name, Sqlitedatabase.cursorfactory factory, int version) |
abstract void onCreate (sqlitedatabase db) |
called when creating a database |
abstract void Onupgrade (sqlitedatabase db, int oldversion, int new Version) |
call |
void OnOpen (Sqlitedatabase db) when you upgrade the database; |
call when opening the database |
sqlitedatabase getreadabledatabase () |
Create or open a database |
sqlitedatabase ge Twritabledatabase () |
Create or open a read-write database |
Here is a concrete example: Databasehelper.java
Private Static classDatabasehelperextendsSqliteopenhelper {Static FinalString database_name = "College.db"; Static Final intDatabase_version = 1; Static FinalString students_table_name = "STUDENTS"; Static FinalString create_db_table = "CREATE TABLE" + Students_table_name + "(_id INTEGER PRIMARY KEY AutoIncrement, "+" Name text NOT NULL, "+" grade text is not null); "; Databasehelper (Context context) {Super(Context, database_name,NULL, database_version); } @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (create_db_table); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {Db.execsql ("DROP TABLE IF EXISTS" +students_table_name); OnCreate (DB); }}
(ii) creation of a database
Creating a Database object in activity may be created in the OnCreate () method or event:
New Databasehelper (thisnull= Database.getwritabledatabase ();
You first instantiate an object that sqliteopenhelper the helper class, and then call the object's Getwritabledatabase () or Getreadabledatabase () method to get the Sqlitedatabase database object.
Another 2 ways to create:
1, apply Sqlitedatabase object OpenDatabase () method and Openorcreatedatabase () method to create the database;
2, the application activity inherits from the parent class Android.content.Context Create the Database Method Openorcreatedatabase () to create the database.
(iii) Sqlitedatabase class
Method |
Description |
Openorcreatedatabase (String path, Sqlitedatabase.cursorfactory Factory) |
Open or create a database |
OpenDatabase (String path, sqlitedatabase.cursorfactory factory,int flags) |
Open the specified database |
Insert (string table, String nullcolumnhack, contentvalues values) |
Add a record |
Delete (String table,string whereclause, string[] whereargs) |
Delete a record |
Update (String table,contentvalues values, string whereclause,string[] Whereargs) |
Modify a record |
Query (string table,string[] columns, string selection, String[]selectionargs, String groupby,string Having, string-by-clause) |
Query a record |
Execsql (String sql) |
Execute an SQL statement |
Close () |
Close the database |
(iv) Increased deletion and inspection
SQLite applications in Android