Sqliteopenhelper API Explanation:
- A Helper class to manage database creation and version management.
- You create a subclass implementing
onCreate(android.database.sqlite.SQLiteDatabase)
, and onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
optionally onOpen(android.database.sqlite.SQLiteDatabase)
, and this class takes care of opening the database if It exists, creating it does not, and upgrading it as necessary. Transactions was used to make sure the database was always in a sensible state.
What do these two big strings of English mean? We translate in one sentence:
First sentence: An auxiliary class to manage database creation and versioning.
----> means to create, connect, and maintain databases
The second sentence: you create a subclass implementation OnCreate (Android.database.sqlite.SQLiteDatabase)
Onupgrade (Android.database.sqlite. Sqlitedatabase,int,int)
and optional OnOpen (android.database.sqlite.SQLiteDatabase)
And if there is such a responsibility to open the database, if it does not create, the upgrade is necessary. Transactions are used to ensure that the database is always in a reasonable state.
----> that is to say: 1. Inherit sqliteopenhelper with one class
2. Abstract methods OnCreate () and Onupgrade () methods must be implemented
3. If there is a database, the class can be responsible for opening the database.
4. If the database cannot be created again, the upgrade is necessary. Columns such as: When the database table structure is to add a column, use the upgrade.
5. All of these operations are done to ensure that the database is in a reasonable state. Columns such as: New and old users, new and old version of the data storage, add, modify processing.
Because Sqliteopenhelper does not have an empty constructor, an inherited class cannot automatically invoke the default parameterless constructor, so an explicit constructor must be defined.
For example, we write a mysqliteopenhelper inheritance Sqliteopenhelper
Public classMysqliteopenhelperextendssqliteopenhelper{ PublicMysqliteopenhelper (context context, String name,cursorfactory factory,intversion) { Super(context, name, Factory, version); } @Override Public voidonCreate (Sqlitedatabase db) {} @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { }}
Now, let's take a look at this constructor Mysqliteopenhelper (context context, String name,cursorfactory factory, int version)
Api:
Create a helper object to create, open, and/or manage a database. The database is not a actually created or opened until one of getWritableDatabase()
or is getReadableDatabase()
called.
Create a helper object to create, open, and/or manage the database. The database is not created or opened until either getwritabledatabase () or getreadabledatabase () is called.
In other words: 1. Create a helper 2. The Helper calls Getwritabledatabase () or getreadabledatabase () to create and open a database .
- A context context (to using to open or create the database) is used to open or create a
- Name Database name
- Cursorfactory cursor (to creating cursor objects, or NULL for the default) null
- Version Database revision number
Four parameters, the latter three can be declared directly in this class. As we know earlier, activity is a context. That is, when the activity calls this class, we just have to pass in an activity.
So we modify this constructor as:
Public classMysqliteopenhelperextendssqliteopenhelper{Private Static FinalString name = "Mydata.db";//Database name Private Static Final intVersion = 2;//Database Version PublicMysqliteopenhelper (Context context) {Super(Context, Name,NULL, version); } @Override Public voidonCreate (Sqlitedatabase db) {} @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { }}
In the activity we can use this:
public class mainactivity extends Activity {@Override protected void OnCreate (Bundle Savedinstancestate) { super .oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); // get helper Mysqliteopenhelper Openhelper = new mysqliteopenhelper (mainactivity. This Mysqliteopenhelper (Getapplicationcontext ())
// Create or open a database
Sqlitedatabase db =
// Close the database
}
You might see this, and some people think of file files. Now we create a new Java project [New-->project-->java Project]
Public class File1 { publicstaticvoidthrows Exception { // declares a file new file ("Info.text"); // generate file New fileoutputstream (file); // Close IO fos.close (); }}
When we comment out the 11th line, run-->java application, then remove the comment and run it once
We found that the new file () did not produce a document. This is what I wrote in my note: a statement. The new FileOutputStream () creates the file.
Similarly, openhelper.getreadabledatabase (); is to actually create, open the database.
Filed under: Android Sqliteopenhelper (ii) http://www.cnblogs.com/hxb2016/p/6118475.html
Thank you for your attention (there was something too busy). From today onwards, poetry will be over. A classic quote for a while: the only constant in the world is that everything is changing.
Android Sqliteopenhelper (i)