Sqlite3 database operation notes for Android (2)-sqliteopenhelper

Source: Internet
Author: User

Before using sqlite3, I did not notice the sqliteopenhelper class, so I had to create and maintain databases and data tables in the activity.

However, with the sqliteopenhelper class, you can separate the maintenance of databases, data tables, and some initialized data from the activity...

The creation of the database and data table structure only needs to be performed once. Opening the database to obtain the corresponding sqlitedatabase operation class of the database may be performed every timeProgramThey all need to be executed. How can we reasonably put these two steps into a helper class? Sqliteopenhelper! Wooden fault! This class only needs to inherit from this class. Call the constructor sqliteopenhelper (context, string name, cursorfactory factory, int version) and rewrite oncreate (sqlitedatabase dB) and onupgrade (sqlitedatabase dB, int oldversion, int newversion ...).

The following describes the general principle of this class. Assume that your sqlitehelper inherits from the sqliteopenhelper class and calls the sqliteopenhelper constructor and implements oncreate and onupgrade disconnection. When you call getwritabledatabase () in the program () method, it will automatically check your databases directory. If there is no database file you need to open, it will automatically call the oncreate method you wrote, then, return the representation of the database you created. if it already exists, it will directly return the representation of the database. In this way, we can put the initialized database table and the default data in the oncreate function for implementation...

The inherited class constructor must call the constructor sqliteopenhelper (context, string name, cursorfactory factory, int version) of the parent class (sqliteopenhelper ).

Context is used to open and create a database, and name is the file name of the database. If the factory is set to null, the default version is used. version is the version number of the created or opened database,The value must be greater than or equal to 1.

If the version is different from the version that was last opened, sqliteopenhelper automatically calls the onupgrade method ..

By the way, if the database is opened in the activity, remember to close it !!!


Test one of sqliteopenhelperCodeFramework,

Sqlitehelper. Java

Package COM. yao_guet.test; import android. content. context; import android. database. sqlexception; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqlitedatabase. cursorfactory; import android. database. SQLite. sqliteopenhelper; import android. util. log;/*** sqlite3 database helper class * @ author Yao. guet * blog: http://blog.csdn.net/Yao_GUET * Date: */public class sqlitehelper exten DS sqliteopenhelper {private final static string tag = "sqlitehelper"; Public sqlitehelper (context, string name, cursorfactory factory, int version) {super (context, name, factory, version ); // todo auto-generated constructor stub} @ overridepublic void oncreate (sqlitedatabase dB) {// todo auto-generated method stublog. E (TAG, "sqlitehelper oncreate! "); Try mongodb.exe csql (" create table data ("+" id integer primary key autoincrement, "+" username varchar (50) "+") "); log. E (TAG, "createdatatable OK! ");} Catch (sqlexception SE) {se. printstacktrace () ;}@ overridepublic void onopen (sqlitedatabase dB) {// todo auto-generated method stublog. E (TAG, "sqlitehelper on open! "); Super. onopen (db) ;}@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stublog. E (TAG, "sqlitehelper onupgrade! ");}}

Test Activity

Sqlitehelpertest. Java

Package COM. yao_guet.test; import android. app. activity; import android. database. SQLite. sqlitedatabase; import android. OS. bundle; import android. util. log; import android. view. keyevent; import android. widget. button; import android. widget. textview; public class sqlitehelpertest extends activity {private final static string tag = "sqlitehelpertest"; private sqlitehelper sqlhelper; private sqlitedatabase dB; @ over Rideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. sqlitehelper_test); sqlhelper = new sqlitehelper (this, "test2.db", null, 2); DB = sqlhelper. getwritabledatabase () ;}@ overrideprotected void ondestroy () {// todo auto-generated method stublog. E (TAG, "ondestroy! "); If (DB! = NULL) dB. close (); super. ondestroy () ;}@ overrideprotected void onpause () {// todo auto-generated method stublog. E (TAG, "onpause"); super. onpause ();}}

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.