public abstract void Onupgrade (Sqlitedatabase db,int oldversion,int new Version)
This method needs to be re-written. The Onupgrade method is used when the repository needs to be upgraded. Can be used to add a table or any other action. If you want to add a new column you can use ALTER TABLE to insert the table. If you need to rename or delete a column, you can use ALTER TABLE to rename the old table, create a new table, and add the old table numbers to the new table. If thrown out of the ordinary, will self-roll. 1. What is the "library upgrade" that is meant to help the files? You have developed a program that was 1.0 versions before. The program uses the repository. By 1.1, you added a field to a table in the repository. Then the software version 1.0 will be upgraded in version 1.1 of the software. 2. What should I pay attention to in the repository? When version 1.0 of software is upgraded to version 1.1, old numbers cannot be dropped. Then in the 1.1 version of the program there will be a place to detect the new version of the software is incompatible with the old repository, and can be able to upgrade the 1.0 software to 1.1 software can be used in the repository. In other words, add that field to the table in the 1.0 software's library and give the word defaults value. 3. How does the program know that the repository needs to be upgraded? The structure of the Sqliteopenhelper has a parameter that is an int version, meaning that it refers to the edition of the Repository. For example, in software version 1.0, when we use Sqliteopenhelper to access the library, the parameter is 1, so the Repository version 1 will be written in our repository. By the 1.1 version, our repositories need to be transformed, so our 1.1 version of the program uses an integer greater than 1 to construct sqliteopenhelper, which is used to access new repositories, such as 2. When our 1.1 new program reads the 1.0 version of the old repository, it finds that the repository stored in the old repositories is 1, and our new program visits it with a version of 2, and the system knows that the repository needs to be upgraded. 4. When do I have a library upgrade? How to upgrade? When the system is building Sqliteopenhelper, if the version is not the same, it will automatically adjust the Onupgrade function so that you can ascend to the repository here. According to the above scene, in this function, the old version of the library's corresponding table to add fields, and to increase the default value for each record. Both the new version and the old version are used as the parameters of the Onupgrade function, and the developers know which version the repository should be upgraded from. After the upgrade is complete, the latest version of the Repository's self-storage is the current version of the repository. --------------------------------------------------------------------------------------------------------------- -- The above is based on the Onupgrade method in Sqliteopenhelper, which is the upgrade of the repository. The context is first taken: Private Context mcontext=this;
And then, in the Finaldb, the method of meditation: Finaldb.create (Mcontext, "afinal.db", true,2,this);
The methods in the Finaldb Dbupdatelistener interface are: @Overridepublic void Onupgrade (sqlitedatabase db, int arg1, int arg2) {//TODO auto-generated method Stubtry {InputStream in = Mcontext.getassets (). Open ("Patch.sql"); BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (in)); String sqlupdate = Null;while ((sqlupdate = Bufferedreader.readline ()) = null) {if (! Textutils.isempty (sqlupdate)) {db.execsql (sqlupdate);}} Bufferedreader.close (); In.close ();} catch (SQLException e) {System.out.println (e.tostring ());} catch (IOException e) {System.out.println (e.tostring ());}}
The time is tense, did not write too much, everybody can study by oneself. |