An example analysis of sqliteopenhelper usage of Android development _android

Source: Internet
Author: User

This example describes the Sqliteopenhelper usage of Android development. Share to everyone for your reference. The specific analysis is as follows:

If you use Openorcreatedatabase (name, mode, factory) in an activity directly in Android development, there is a series of questions to follow. For example, database upgrades, updates, and so on.

It is best to use its encapsulated version: Sqliteopenhelper

The main job of inheriting and extending the Sqliteopenhelper class is to rewrite the following two methods.

OnCreate (Sqlitedatabase db): Executes the method when the database is first created, and typically creates an initialization action such as a table in the method.

Onupgrade (sqlitedatabse dv, int oldversion,int new version): This method is called when the database is opened with a different version number than the current version number.

In addition to the two methods that must be implemented, you can optionally implement the OnOpen method, which is invoked each time the database is opened.

Rewrite these functions in your business, and then use the helper getwritabledatabase and getreadabledatabase to get the database you want to manipulate. You can do it again.

In addition, to determine whether a table exists in SQLite, you can use the following methods:

String sql = "SELECT COUNT (*) from sqlite_master WHERE type= ' table ' and name= ' items '"; 
Cursor cur = db.rawquery (sql, NULL); 
int count =-1; 
while (Cur.movetonext ()) { 
  count = cur.getint (0); 
} 
if (count <= 0) { 
  //table does not exist 
} else {

}

I hope this article will help you with your Android program.

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.