How to Use sqliteopenhelper
Sqliteopenhelper is a helper class for managing database creation and versions.
You can perform some operations on the database by inheriting this class and implementing some of its methods.
All classes that inherit this class must implement the following constructor:
Public databasehelper (context, string name, cursorfactory factory, int Version)
The first parameter is a context object of the context type.
Second parameter: string type, Database Name
Third parameter: cursorfactory type
Fourth parameter: int type, database version
The following are several methods of this class:
Method Name return type description remarks
Getreadabledatabase () synchronized sqlitedatabase creates or opens a database. You can use these two methods to return the sqlitedatabase object and perform a series of operations on the database, such as creating a new table and inserting a data record.
Getwritabledatabase () synchronized sqlitedatabase creates or opens a database that can be read and written.
Oncreate (sqlitedatabase dB) Abstract void is called when it is created for the first time
Onopen (sqlitedatabase dB) void
Onupgrade (sqlitedatabase dB, int oldversion, int newversion) Abstract void upgrade Database
Close () synchronized void close all opened database objects
Example:
Databasehelper class:
Package qyq. sqlitetest. dB;
Import Android. content. context;
Import Android. database. SQLite. sqlitedatabase;
Import Android. database. SQLite. sqlitedatabase. cursorfactory;
Import Android. database. SQLite. sqliteopenhelper;
/**
* Sqliteopenhelper is a helper class used to manage database creation and version. It provides two functions.
* First, getreadabledatabase () and getwritabledatabase () can obtain the sqlitedatabase object, through which you can operate on the database
* Second, two callback functions are provided: oncreate () and onupgrade (), which allow us to perform operations when creating and upgrading databases.
*/
Public class databasehelper extends sqliteopenhelper {
Private Static final int version = 1;
/** Constructor. This constructor is required in the Implementation class of sqliteopenhelper.
* @ Param context: the context object is the activity.
* @ Param name: Database Name
* @ Param Factory
* @ Param version: version of the current database. The value must be an integer and increase progressively.
*/
Public databasehelper (context, string name, cursorfactory factory,
Int version ){
// The constructor in the parent class must be called through Super
Super (context, name, factory, version );
}
// Constructor
Public databasehelper (context, string name, int version ){
This (context, name, null, version );
}
// Constructor
Public databasehelper (context, string name ){
This (context, name, version );
}
// Create or open a read-only database and return the sqlitedatabase object
@ Override
Public synchronized sqlitedatabase getreadabledatabase (){
// Todo auto-generated method stub
Return super. getreadabledatabase ();
}
// Create or open a database that can be read and written, and return the sqlitedatabase object
@ Override
Public synchronized sqlitedatabase getwritabledatabase (){
// Todo auto-generated method stub
Return super. getwritabledatabase ();
}
// This method is executed when the database is created for the first time. This method is called only when the sqliteopenhelper object is obtained for the first time.
@ Override
Public void oncreate (sqlitedatabase dB ){
// Todo auto-generated method stub
System. Out. println ("oncreate method ");
// Execsql is used to execute SQL statements and create database tables.
Db.exe csql ("create table users (ID int, name varchar (50 ))");
}
@ Override
Public void onopen (sqlitedatabase dB ){
// Todo auto-generated method stub
System. Out. println ("onopen method ");
Super. onopen (db );
}
@ Override
Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){
System. Out. println ("onupgrade method ");
}
Activity Type
Package qyq. sqlitetest;
Import qyq. sqlitetest. DB. databasehelper;
Import Android. App. activity;
Import Android. content. contentvalues;
Import Android. database. cursor;
Import Android. database. SQLite. sqlitedatabase;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. edittext;
Public class sqlitetestactivity extends activity {
/** Called when the activity is first created .*/
// Control declaration
Private button createdatabase = NULL;
Private button updatedatabase = NULL;
Private button insert = NULL;
Private button update = NULL;
Private button query = NULL;
Private button Delete = NULL;
Private edittext id = NULL;
Private edittext name = NULL;
Private int inputid = 0;
Private string inputname = NULL;
// Obtain the onclicklistener listener of the control.
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Createdatabase = (button) findviewbyid (R. Id. createdatabase );
Createdatabase. setonclicklistener (New createdatabaselistener ());
Updatedatabase = (button) findviewbyid (R. Id. updatedatabase );
Updatedatabase. setonclicklistener (New updatedatabaselistener ());
Id = (edittext) findviewbyid (R. Id. ID );
Name = (edittext) findviewbyid (R. Id. Name );
Insert = (button) findviewbyid (R. Id. insert );
Insert. setonclicklistener (New insertlistener ());
Update = (button) findviewbyid (R. Id. Update );
Update. setonclicklistener (New updatelistener ());
Query = (button) findviewbyid (R. Id. query );
Query. setonclicklistener (New querylistener ());
Delete = (button) findviewbyid (R. Id. delete );
Delete. setonclicklistener (New deletelistener ());
System. Out. println ("test ");
}
// Listener class
Class createdatabaselistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// A databasehelper object is created. If you only execute this statement, no connection is created or opened.
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test ");
// A connection is created or opened only after the getwritabledatabase () method or getreadabledatabase () method of databasehelper is called.
Sqlitedatabase DB = helper. getreadabledatabase ();
}
}
// Listener class
Class updatedatabaselistener implements onclicklistener {
@ Override
Public void onclick (view v ){
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test", 2 );
Sqlitedatabase DB = helper. getwritabledatabase ();
}
}
// Listener class
Class insertlistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// Create a contentvalues object
Contentvalues values = new contentvalues ();
// Insert a key-value pair to this object. The key-value pair is the column name and the value is the value to be inserted into this column. The value must be of the same type as the data in the database.
// Integer. parseint (Id. gettext (). tostring () Get the value to be inserted from the screen
Values. Put ("ID", integer. parseint (Id. gettext (). tostring ()));
Values. Put ("name", name. gettext (). tostring ());
// Create a databasehelper object
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test ");
// Get a writable sqlitedatabase object
Sqlitedatabase DB = helper. getwritabledatabase ();
// Call the insert method to insert data to the database.
// First parameter: Table Name
// The second parameter: SQL does not allow an empty column. If contentvalues is empty, this column is explicitly specified as a null value.
// The third parameter: contentvalues object
DB. insert ("users", null, values );
}
}
// Listener class
Class updatelistener implements onclicklistener {
@ Override
Public void onclick (view v ){
Contentvalues values = new contentvalues ();
Values. Put ("name", "lisiyuan ");
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test ");
Sqlitedatabase DB = helper. getwritabledatabase ();
String id1 = ID. gettext (). tostring ();
// Call the update Method
// The first parameter string: Table Name
// The second parameter contentvalues: contentvalues object
// The third parameter, string: Where, is equivalent to the statement after SQL statement where ,? Number is a placeholder
// The fourth parameter string []: The placeholder Value
DB. Update ("users", values, "id =? ", New string [] {id1 });
}
}
// Listener class
Class querylistener implements onclicklistener {
@ Override
Public void onclick (view v ){
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test ");
Sqlitedatabase DB = helper. getreadabledatabase ();
// Call the query method of the sqlitedatabase object to query and return a cursor object: The result set object returned by the database query
// The first parameter string: Table Name
// The second parameter string []: name of the column to be queried
// The third parameter string: Query Condition
// Fourth parameter string []: Query condition parameter
// The Fifth parameter string: grouping query results
// String of the sixth parameter: restrict the grouping results
// String of the seventh parameter: sorts the query results.
Cursor cs = dB. Query ("users", new string [] {"ID", "name"}, "id =? ", New string [] {" 5 "}, null );
// Move the cursor to the next row to determine whether there is any data in the result set. If yes, true is returned. If no, false is returned.
While (CS. movetonext ()){
System. Out. println (CS. getstring (CS. getcolumnindex ("name ")));
}
}
}
// Listener class
Class deletelistener implements onclicklistener {
@ Override
Public void onclick (view v ){
Databasehelper helper = new databasehelper (sqlitetestactivity. This, "sqlite_test ");
Sqlitedatabase DB = helper. getwritabledatabase ();
String id1 = ID. gettext (). tostring ();
// Call the delete method of the sqlitedatabase object to delete the object
// The first parameter string: Table Name
// The second parameter, string: Where, is equivalent to the statement after SQL statement where ,? Number is a placeholder
// The third parameter string []: The placeholder Value
DB. Delete ("users", "id =? ", New string [] {id1 });
}
}
}
}