SqliteOpenHelper's onUpgrade () Solution

Source: Internet
Author: User

The android 2.3 and Xiaomi System of your own machine have been successfully executed once, and the versions passed before and after are correct. If it is not executed, it is indeed the getWritableDatabase () called (), no exception was found after reading the Android source code.

Think twice! I switched to a testing machine of the company.

Therefore, it is determined that this onUpgrade is unreliable and you have written a set of logic for upgrading the sqlite database.

Create an update. SQL file under assets, read the file, and execute SQL statements in one row. If the upgrade succeeds, write a mark in the database.

Note: In update. SQL, a complete operation includes a sentence that needs to be merged into a row.

Code ):

Private void updateDB () {SqliteHelper sqliteHelper = SqliteHelper. getInstance (this); if (! ("" + Config. SQLITE_DB_VERSION ). equals (sqliteHelper. getValue (Configuration. DB_VERSION) {LogUtil. I (TAG, "===== Database Upgrade start ===="); // sqliteHelper. beginTransaction (); try {InputStream in = this. getAssets (). open ("update. SQL "); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (in); String tmpStr = null; StringBuffer SQL = new StringBuffer (); while (tmpStr = bufferedReader. read Line ())! = Null) {SQL. append (tmpStr); SQL. append ("\ n"); if (tmpStr. trim (). endsWith (";") {sqliteHelper.exe cSQL (SQL. toString (); SQL = new StringBuffer () ;}} bufferedReader. close (); in. close (); // sqliteHelper. setTransactionSuccessful (); sqliteHelper. setValue (Configuration. DB_VERSION, "" + Config. SQLITE_DB_VERSION); LogUtil. I (TAG, "===== Database Upgrade successful! ==== ");} Catch (Exception e) {LogUtil. e (TAG, e. getMessage ();} finally {// sqliteHelper. endTransaction ();}}}

 

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.