How to use your database in Android

Source: Internet
Author: User

Mobile application development often used to store some data in the database or data cache, Android provides us with a lightweight database, in the upper layer of a package, but also provides us with a contentprovider framework for us to do data manipulation, and data sharing between different programs. This article describes some of the things I think are good habits in using the database, and welcome to discuss with me.

About the framework

Usually the network operation, JSON parsing, I will use the framework, which can help me to handle the exception, processing asynchronous operations. But I use my own sqlitehelper and ContentProvider for database operations, so that the Android system offers us a layer of encapsulation on SQLite. Therefore, I no longer use the third-party SQLite framework. 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.

As you can see, the operations that Android has provided for us have been encapsulated, and there are some similarities in many places and other ORM frameworks. And in Android we don't usually store very complex data structures, and there's no need to add cost to your own learning framework.

Principles of database Building and library upgrading

Look at a piece of code first

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 sqlitedatabase db) {if (CONSTANTS.LOGVV) {            LOG.V (Constants.tag, "populating New Database");        } onupgrade (db, 0, db_version); }/** * Updates the database format when a content provider are used * with a database that was Crea         Ted 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 sqlitedatabase DB, Int. 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, "INTE                    GER 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" + 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.

Other, this article, I disorderly pull. If you have any objection, please reply to the discussion.

Original address: http://blog.isming.me/2014/10/13/good-database-use/, reproduced please indicate the source.

How to use your database in Android

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.