In front of the use of SQLite3, and did not notice the Sqliteopenhelper this class, so we have to create and maintain in the activity database and data table creation.
However, with the Sqliteopenhelper class, it is possible to separate the maintenance of the database and the data tables and some of the initialized data from the activity ...
Database and data table structure is created only once, and open the database to get the database corresponding to the Sqlitedatabase operation class is likely to be executed each time the program needs to execute, how to put these two steps reasonable into a helper class? sqliteopenhelper! Wood wrong! Just this class, just inherit the class, call the constructor Sqliteopenhelper (context context, String name, Cursorfactory factory,int version) and rewrite OnCreate ( Sqlitedatabase db) and Onupgrade (sqlitedatabase db, int oldversion, int newversion) are just two methods (but as if I'm only using OnCreate ...). )。
Here is the general principle of this class, assuming that your sqlitehelper inherits from the Sqliteopenhelper class, calls the Sqliteopenhelper constructor and implements OnCreate and Onupgrade, When you call the Getwritabledatabase () method in the program, it will automatically check your databases directory, if there is no database file you need to open, it will automatically call the method you wrote OnCreate, and then return to the database representation you created, If it already exists, it will return the representation of the database directly. In this way, we initialize the database table, with the default data can be put into the OnCreate function to implement ...
The constructor of the parent class (Sqliteopenhelper) must be called inside the inherited class constructor Sqliteopenhelper (context context, String name, Cursorfactory factory,int Version).
The context is used to open the CREATE database library, name is the file name of the database, factory is set to NULL using the default, version is the number of the database created or opened, which must be greater than or equal to 1
If this time version is inconsistent with the last version opened, Sqliteopenhelper will automatically call the Onupgrade method.
Yes, in the activity, if you open the database, be sure to remember to close!!!
Test a code framework for Sqliteopenhelper,
Sqlitehelper.java
Packagecom. Yao_guet.test; ImportAndroid.content.Context; Importandroid.database.SQLException; Importandroid.database.sqlite.SQLiteDatabase; Importandroid.database.sqlite.SQLiteDatabase.CursorFactory; ImportAndroid.database.sqlite.SQLiteOpenHelper; ImportAndroid.util.Log; /*** SQLite3 Database helper class *@authorYao.guet * Blog:Http://blog.csdn.net/Yao_GUET* date:2011-07-06*/ Public classSqlitehelperextendsSqliteopenhelper {Private Final StaticString TAG = "Sqlitehelper"; PublicSqlitehelper (Context context, String name, Cursorfactory factory,intversion) { Super(context, name, Factory, version); //TODO auto-generated Constructor stub} @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method StubLOG.E (TAG, "Sqlitehelper oncreate!"); Try{db.execsql ("Create TABLE Data (" + "ID integer Primary Key autoincrement," + "use Rname varchar (50) "+") "); LOG.E (TAG,"Createdatatable ok!"); } Catch(SQLException se) {se.printstacktrace (); }} @Override Public voidOnOpen (Sqlitedatabase db) {//TODO auto-generated Method StubLOG.E (TAG, "Sqlitehelper on open!"); Super. OnOpen (DB); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO auto-generated Method StubLOG.E (TAG, "Sqlitehelper onupgrade!"); } }
Test activity
Sqlitehelpertest.java
Packagecom. Yao_guet.test; Importandroid.app.Activity; Importandroid.database.sqlite.SQLiteDatabase; ImportAndroid.os.Bundle; ImportAndroid.util.Log; Importandroid.view.KeyEvent; ImportAndroid.widget.Button; ImportAndroid.widget.TextView; Public classSqlitehelpertestextendsActivity {Private Final StaticString TAG = "Sqlitehelpertest"; PrivateSqlitehelper SqlHelper; PrivateSqlitedatabase DB; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (r.layout.sqlitehelper_test); SqlHelper=NewSqlitehelper ( This, "Test2.db",NULL, 2); DB=sqlhelper.getwritabledatabase (); } @Overrideprotected voidOnDestroy () {//TODO auto-generated Method StubLOG.E (TAG, "ondestroy!"); if(db! =NULL) Db.close (); Super. OnDestroy (); } @Overrideprotected voidOnPause () {//TODO auto-generated Method StubLOG.E (TAG, "OnPause"); Super. OnPause (); } }
Operation of "Android" Android under SQLite3 database