Using Databases in Android

Source: Internet
Author: User

Sqlitedatabase and ContentProvider give us a little bit of a function

Query ()//Inquiry Insert ()//Insert Delete ()//delete update ()//update//parameter and return value I didn't write it.
Principles of database Building and library upgrading
private final class databasehelper extends sqliteopenhelper {         public databasehelper (Final context context)  {             super (context, db_name, null, db_ VERSION);        }          /**        * 1-->2 add header table         * 2-->3 update info         * 3--> update info haha         *        */         public static final int db_version = 4;         public static final string db_name =  "Download";          /**         * creates database the first  time we try to open it.         */          @Override          Public void oncreate (FINAL&NBSP;SQLITEDATABASE&NBSP;DB)  {             if  (CONSTANTS.LOGVV)  {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.V (constants.tag,  "populating new  Database ");            }             onupgrade (db, 0, db_version);         }         /**         *  updates the database format when a content provider is used          * with a database that was  created with a different format.          *         * note: to support downgrades , creating a table should always drop it first if it  already         * exists.          */         @Override          public void onupgrade (FINAL&NBSP;SQLITEDATABASE&NBSP;DB,&NBSP;INT&NBSP;OLDV,  final int newV)  {             for  (int  version = oldv + 1; version <= newv; version++)  {                 upgradeto (db, version );            }         }         /**          * Upgrade database from  (version - 1)  to  version.         */         private void upgradeto (sqlitedatabase db, int version)  {             switch  (version)  {                 case 1:                     createdownloadstable (db);                     break;                 case 2:                      createheaderstable (db);                     break;                 case 3:                     addcolumn (db, db_table,  downloads.impl.column_is_public_api,                                "Integer not null default  0 ");                     addcolumn (db, db_table, downloads.impl.column_allow_roaming,                                 "integer not null default 0");                      addcolumn (db, db_table, downloads.impl.column_allowed_network_types,                                 "integer not null default 0");                     break;                 case 103:                      AddColumn (db, db_table, downloads.impl.column_is_visible_in_downloads_ui,                                 "integer not null default 1");                      Makecachedownloadsinvisible (db);                     break;                 case 4:                     addcolumn (db, db_table, downloads.impl.column_bypass_recommended_size_ limit,                              "Integer not null default  0 ");                     break;                 default:                     throw new illegalstateexception ("Don ' t know how  To upgrade to&nbsP; "  + version);            }         }

The above code is excerpted from the Downloadprovider databasehelper code in Android. I am here mainly like to recommend this database of the table, the initial contact with this form is in the previous reading Downloadmanager, found that the design of Android is really very subtle. This way, easy to upgrade the database, in the update database and create database, you can share the table, modify the data table code, while you can clearly see the changes in the database.

At the same time, it is suggested that when modifying the database version, add comments on the version number, write the contents of the database upgrade, so that you can see the changes in the database later, and others look at the code, understand the changes in the database.

Database building tables and data storage recommendations

Some simple configuration files, not recommended to the database, stored in the sharepreference, easy access, but also improve access speed.

files, pictures, etc. absolutely do not save to the database, store the file path to the database.

Some very complex data, it is recommended to go directly to JSON to save to the database. Some caches can also be stored in this way.

What else to say.

Do not operate on the main thread while the database is operating. This is a time-consuming operation that easily results in ANR.

When data is displayed in the database, it is recommended to use with Cursorloader, which is the asynchronous data load provided by Android, and automatically refreshes the data when the data changes.


Using Databases in Android

Related Article

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.