Four Data Storage Methods for Android _ Android

Source: Internet
Author: User

The Android system provides four data storage methods. They are SharePreference, SQLite, Content Provider, and File. In Android, data is basically private and stored in the "data/package name" directory. Therefore, to share data, use Content Provider. SQLite: SQLite is a lightweight database that supports basic SQL syntax and is a common data storage method. Android provides a class named SQLiteDatabase for this database, which encapsulates APIs for database operations. SharedPreference: in addition to the SQLite database, SharedPreference is another common data storage method. In essence, SharedPreference is an xml file, which is often used to store simple parameter settings. File: A File (I/O) storage method. It is often used to store a large amount of data, but the disadvantage is that updating data will be a difficult task. ContentProvider: a data storage method that can be shared by all applications in the Android system. Because data is usually private among applications, this storage method is rarely used, however, it is an essential storage method. For example, audio, video, image, and address book can be stored in this way. Each Content Provider provides a public URI (encapsulated as a Uri object). If the application needs to share data, the Content Provider needs to define a URI for the data, then other applications pass in the URI through the Content Provider to operate the data. PS: URI consists of three parts: "content: //", data path, and ID (optional ). SQLite is a lightweight database designed for embedded devices. It has only five data types: null integer: integer real: floating point TEXT: String BLOB: big Data in SQLite does not specifically design the BOOLEAN and DATE types, because the BOOLEAN type can replace true and false with INTEGER 0 and 1, the DATE type can have TEXT, REAL, and INTEGER values in a specific format instead of display. To facilitate the operation of the DATE type, SQLite provides a set of functions. For details, see http://www.sqlite.org/lang_datefunc.html. This simple data type design is more in line with the requirements of embedded devices. For more information about SQLite, see: http://www.sqlite.org/Android system provides the android. database. sqlite package for SQLite database to add, delete, modify, query work. The main method is as follows: beginTransaction (): Start a transaction. Close (): close the connection and release resources. Delete (String table, String whereClause, String [] whereArgs): delete records that meet the specified conditions. EndTransaction (): ends a transaction. ExecSQL (String SQL): Execute the given SQL statement. Insert (String table, String nullColumnHack, ContentValues values): insert a record based on the given conditions. OpenOrCreateDatabase (String path, SQLiteDatabase. CursorFactory factory): connects to the database based on the given conditions. If the database does not exist, it is created. Query (String table, String [] columns, String selection, String [] selectionArgs, String groupBy, String having, String orderBy): Execute the query. RawQuery (String SQL, String [] selectionArgs): executes the query based on the given SQL. Update (String table, ContentValues values, String whereClause, String [] whereArgs): Modify the records that meet the conditions according to the given conditions. In addition to the main appeal method, Android also provides many practical methods. In a word, it is very convenient for Android to access the database.

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.