Android sqliteopenhelper example

Source: Internet
Author: User

Android sqliteopenhelper example
 
Author: byandby Source: www.sqlite.com.cn time: 2011-8-16 [Font: large medium small ]〖
Double-click screen Rolling 〗
 
 
 

We all know that the Android platform provides us with a database helper class to create or open databases. This helper class inherits from the sqliteopenhelper class. In the constructor of this class, call the method in context to create and open a database object with the specified name. The main task of inheriting and extending the sqliteopenhelper class is to rewrite the following two methods.

Oncreate (sqlitedatabase dB): When a database is created for the first time, this method is generally executed in this method for initialization operations such as table creation.
Onupgrade (sqlitedatabse DV, int oldversion, int new version): This method is called when the version number passed in when the database is opened is different from the current version number.

In addition to the two methods that must be implemented, the onopen method can also be selectively implemented. This method will be called each time the database is opened.

The basic usage of the sqliteopenhelper class is: when you need to create or open a database and obtain database objects, first create a secondary object based on the specified file name, call the getwritabledatabase or getreadabledatabase method of the object to obtain the sqlitedatabase object.

Calling the getreadabledatabase method does not always return read-only database objects. Generally, this method returns the same information as the getwriteabledatabase method, a read-only database object is returned only when the database is only available for read-only or when the disk is full.

The following is a simple example to describe the usage of sqliteopendatabase, including creating a database, inserting data, updating, and querying. We will display the data obtained after the query to textview, check the running result.

Package xiaohang. zhimeng;
 
Import Android. content. context;
Import Android. database. SQLite. sqlitedatabase;
Import Android. database. SQLite. sqliteopenhelper;
Import Android. database. SQLite. sqlitedatabase. cursorfactory;
 
Public class mysqlitehelper extends sqliteopenhelper {
// Call the parent class Constructor
Public mysqlitehelper (context, string name, cursorfactory factory,
Int version ){
Super (context, name, factory, version );
}
 
/**
* This method is executed when the database is first created. Initialization operations such as table creation are generally executed in this method.
* Override the oncreate method and call the execsql method to create a table.
**/
@ Override
Public void oncreate (sqlitedatabase dB ){
Db.exe csql ("create table if not exists hero_info ("
+ "Id integer primary key ,"
+ "Name varchar ,"
+ "Level integer )");
 
}
 
// This method is called when the database is opened and the version number is different from the current version number.
@ Override
Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){
}
 
}

Package xiaohang. zhimeng;
 
Import Android. App. activity;
Import Android. content. contentvalues;
Import Android. database. cursor;
Import Android. database. SQLite. sqlitedatabase;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. widget. textview;
 
Public class activity01 extends activity {
Mysqlitehelper myhelper;
Textview TV;
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
TV = (textview) findviewbyid (R. Id. TV );
// Create a mysqliteopenhelper helper class Object
Myhelper = new mysqlitehelper (this, "My. DB", null, 1 );
// Insert and update data to the database
Insertandupdatedata (myhelper );
// Query data
String result = querydata (myhelper );
TV. settextcolor (color. Red );
TV. settextsize (20366f );
TV. settext ("Name \ t Level \ n" + result );
 
}
 
// Insert and update data to the database
Public void insertandupdatedata (mysqlitehelper myhelper ){
// Obtain the database object
Sqlitedatabase DB = myhelper. getwritabledatabase ();
// Insert data to the table using the execsql Method
Db.exe csql ("insert into hero_info (name, level) values ('bb', 0 )");
// Insert data into the table using the insert method
Contentvalues values = new contentvalues ();
Values. Put ("name", "XH ");
Values. Put ("level", 5 );
// Call the method to insert data
DB. insert ("hero_info", "ID", values );
// Use the update method to update table data
// Clear the contentvalues object
Values. Clear ();
Values. Put ("name", "XH ");
Values. Put ("level", 10 );
// Update the XH level to 10.
DB. Update ("hero_info", values, "level = 5", null );
// Close the sqlitedatabase object
DB. Close ();
}
 
// Query data from the database
Public String querydata (mysqlitehelper myhelper ){
String result = "";
// Obtain the database object
Sqlitedatabase DB = myhelper. getreadabledatabase ();
// Query the data in the table
Cursor cursor = dB. Query ("hero_info", null, "id ASC ");
// Obtain the index of the name column
Int nameindex = cursor. getcolumnindex ("name ");
// Obtain the index of the level column
Int levelindex = cursor. getcolumnindex ("level ");
For (cursor. movetofirst ();! (Cursor. isafterlast (); cursor. movetonext ()){
Result = Result + cursor. getstring (nameindex) + "\ t ";
Result = Result + cursor. getint (levelindex) + "\ n ";
}
Cursor. Close (); // close the result set.
DB. Close (); // close the database object
Return result;
}
 
@ Override
Protected void ondestroy (){
Sqlitedatabase DB = myhelper. getwritabledatabase (); // obtain the database object
// Delete all data in the hero_info table. Input 1 to delete all rows. ------> click back.
DB. Delete ("hero_info", "1", null );
Super. ondestroy ();
}
}

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.