The wording of the Android database upgrade

Source: Internet
Author: User
<span id="Label3"></p><p><p>Add the upgrade operation to the base class Table:</p></p><p><p></p></p><pre name="code" class="java">Public abstract class dbbasetable {private static final String TAG = "dbbasetable";/** * @return The DB table name */abstr Act String getName ();/** * creates the DB table according to the DB scheme * * @param db */abstract void OnCreate (sqlited Atabase db), void onupgrade (sqlitedatabase db, int oldversion, int newversion, String tempname) {//rename o LD table to temporary name<strong>dbutils.renametable (db, getName (), tempname);</strong>//create Clear Table According to the new scheme <strong> onCreate (db);</strong>//copy content of the matching Columns from the old table to the new one <strong> joincolumns (db, tempname, getName ());</strong> Delete old table <strong> dbutils.droptable (db, Tempname);</strong>//this is a few updates The contents of the table Inittablecontent (db); } void inittablecontent (sqlitedatabase db) {} void joincolumns (sqlitedatabase db, String tempName, String tableName) {<strong>dbutils.joincolumns (db, tempname, tableName);</strong>}} </pre><br><pre name="code" class="java">Final class Dbutils {private static final String TAG = "dbutils"; Private static Final Boolean DEBUG = false; private static final String sqlite_stmt_list_tables = "select name from sqlite_master WHERE type= ' table ' and name N OT like ' sqlite_% ' and ' name ' is not a like ' android% '; private static final String Sqlite_table_name_column = "NAME"; private static final String sqlite_stmt_template_list_columns = "select * from%s LIMIT 1"; private static final String sqlite_stmt_template_drop_table = "DROP TABLE IF EXISTS%s"; private static final String sqlite_stmt_template_rename_table = "ALTER TABLE%s RENAME to%s"; private static final String Sqlite_stmt_template_copy_columns = "INSERT into%s (%s) select%s from%s"; /** * @param db * @return Collection object containing table names in the database */static collection&lt ; string> listtables (sqlitedatabase db) {cursor cursor = db.rawquery (sqlite_stmt_list_tables, null); if (cursor = = NULL | |!cursor.movetofirst ()) {if (cursor! = Null) {cursor.close (); } return null; } int table_name_column = Cursor.getcolumnindex (sqlite_table_name_column); hashset<string> tables = new hashset<string> (cursor.getcount ()); Do {tables.add (cursor.getstring (table_name_column)); } while (cursor.movetonext ()); Cursor.close (); Return tables; }/** * @param db * @param table * @return List of column names in the DB table */public static list<string> listcolumns (sqlitedatabase db, String table) {cursor cursor = db.rawquery (string.forma T (sqlite_stmt_template_list_columns, table), null); if (cursor = = Null) {return null; } list<string> columns = arrays.aslist (cursor.getcolumnnames ()); Cursor.close (); Return columns; } /** * @param db * @param table */static void droptable (sqlitedatabase db, String table) {DB.E Xecsql (string.format (sqlite_stmt_template_drop_table, TABLE)); } static void Renametable (sqlitedatabase db, string oldname, string NewName) {db.execsql (string.format (SQLI te_stmt_template_rename_table, oldname, newName)); } static void Joincolumns (sqlitedatabase db, string oldtable, string Newtable) {//delete all re Cords in the new table before copying from the old table Db.delete (newtable, null, null); Find columns which exist in both tables arraylist<string> old_columns = new Arraylist<string> (listcol Umns (db, oldtable)); list<string> new_columns = ListColumns (db, newtable); Old_columns.retainall (new_columns); String common_columns = Textutils.join (",", old_columns); Copy records from old table to new table Example:<span style= "fOnt-family: Microsoft Jacob black; ><strong><span style= "font-size:10px;" >insert to Mytest1 (_id,account_id,test1,test3) SELECT _id,account_id,test1,test3 from Mytest1_temp_</span ></strong></span> db.execsql (string.format (sqlite_stmt_template_copy_columns, newtable, common_ columns, common_columns, oldtable)); }}</pre><br>Then overload the Onupgrade method in Dbhelper:<p><p></p></p><p><p></p></p><pre name="code" class="java"> @Override public void Onupgrade (sqlitedatabase db, int oldversion, int. Newversion) {//get table names in the O LD db collection<string> old_tables = Dbutils.listtables (db); if (old_tables = = NULL | | old_tables.size () = = 0) {onCreate (db); Return }//get table names in the new DB set<string> new_tables = DataBaseClass.sRCMDbTables.keySet () ; Try {db.begintransaction (); Remove old tables which is not in the new DB scheme hashset<string> obsolete_tables = new Hashset<s Tring> (); For (String table:old_tables) {if (!new_tables.contains (table)) {System.out.println ("= = = =dbhelp Onupgrade droptable table= "+table); Dbutils.droptable (db, table); Obsolete_tables.add (table); }} Old_tables.removeall (obsolete_tables);Create and upgrade new tables Dbbasetable table_descriptor; For (String table:new_tables) {table_descriptor = DataBaseClass.sRCMDbTables.get (table); Check If the new table exists in the old DB if (old_tables.contains (table)) { String temp_name = gettemptablename (table, old_tables, new_tables); System.out.println ("====dbhelp onupgrade temp_name =" +temp_name); Table_descriptor.onupgrade (db, oldversion, newversion, temp_name); } else {table_descriptor.oncreate (db); }} db.settransactionsuccessful (); } catch (throwable e) {throw new runtimeexception ("DB upgrade failed:" + e.getmessage ()); } finally {db.endtransaction (); } }</pre><br><br><p><p></p></p><p><p>The central idea is:</p></p><p><p>In contrast to the old and new database structure, if there is a table in the old table in the new database, then delete the old</p></p><p><p>Suppose there is a table update in the new Database. You need to update the structure of this table. and copy the data from the old Table.</p></p><p><p>(rename the old table to a name.) Copy the data from the named table to the new Table)</p></p><p><p>The code can be downloaded in http://download.csdn.net/detail/baidu_nod/7684479</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article blog original Article. blogs, without consent, may not be reproduced.</p></p> <p><p>The wording of the Android database upgrade</p></p></span>

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.