Detailed Android Data storage technology

Source: Internet
Author: User
Tags sqlite database

Objective

Learn about Android, data storage is one of the key points, if you do not know the data, then let you play with a no data application, how long can you play? The answer is that this is almost the same as no cell phone. We chat QQ, chat, watch the news, brush the circle of friends and so is to look at the data inside, so the data on Android is how important to us.

Data, now is the big data age, who has data, who can grasp the future, this is very scary, now you use the mobile app has a lot of your data information, big data accumulation can grasp your schedule, life rules and so on.

The data storage has a good technical support, is a good development platform embodiment, if not for a long time to maintain data, then will inevitably be eliminated by the era of development. So there is the concept of long-term data retention, there is instantaneous data the concept of the emergence of what is instantaneous data?

See the name of the meaning, instantaneous means that when the stored data due to program closure or other reasons such as data loss, if you want to send a self-portrait to send a friend circle, but a refresh is not, is not very angry? Gas is not gas, gas, is not trying to hit the phone?

This article mainly introduces three ways to implement data storage in Android:
    1. File storage
    2. Sharedpreference Storage
    3. SQLite database Storage
Here are the default storage locations for data files in three ways:

In the Ddms file explore open the Data folder, switch to Ddms view, click the fileexplorer switch card, into the/data/data/ /files/directory, you can see the file.

Export of DDMS files

Okay, next, we'll analyze how the data is stored. For the file storage location, next to the first file storage. Then go on to sharedpreferences storage and SQLite database storage.

File storage

File storage is the most basic form of storage in Android, and the way I/O is implemented in Java is opened by the Openfileinput () and Openfileoutput () methods provided by the context class. File storage is mainly divided into two kinds of storage, one is internal storage, and the other is external storage.

Memory storage: Uses the Openfileinput () method in the FileInputStream class to read the data and uses the Openfileoutput () method in the FileOutputStream class for writing data.

External storage: Use the getExternalStorageDirectory () method in the Enviroment class to read and write files on external storage.

Simplified:

FileOutputStream-Openfileoutput ()
FileInputStream-Openfileinput ()
Environment-getExternalStorageDirectory ()

One, the internal storage of the write file step

First
Gets the file output stream object FileOutputStream

Second
Use the Openfileoutput (String name, int mode) method of the FileOutputStream class

Third
Call the Write () method of the FileOutputStream object for writing to the file

Fourth
Call the Flush () method, because the write () method is write to the buffer, and the flush () method is called to write the buffered data to the file, emptying the cache

Fifth
The close () method is used to close the FileOutputStream

Second, the internal storage of the read file step

First
Gets the file input stream object FileInputStream

Second
Use the Openfileinput () method of the FileInputStream class to implement

Third
Call the Read () method of the FileInputStream object

Fourth
Call Close () to dismiss the file input stream object

The following code example illustrates these steps, as described above, describing the write file step and the read file step for the internal store, respectively.

Write Data

In the Openfileoutput () method, receive two parameters, one is the file name, the other is the operation mode of the files, there are two main types are mode_private and mode_append. The last Openfileoutput () method throws FileNotFoundException.

Main code steps:

Reading data

Use code to read the contents of the file:

In summary, the method of reading and writing files in Android is the same as the program that implements I/O in Java, providing the Openfileinput () and Openfileoutput () methods to read the files on the device. The output stream is obtained through Context.openfileoutput (), the parameters are file name and storage mode respectively, and the input stream is obtained through Context.openfileinput (), and the parameter is the file name.

Describes internal storage and now describes external storage.

Externally stored files are globally readable, and in programs, use the getExternalStorageDirectory method of the Environment class to read external files.

The last external storage file, to add read and write permissions, adds

Read_external_storage
Write_external_storage

So the file storage is about the same, try to practice more. For sharepreferences storage and SQLite storage, go to the tutorial now.

The following is an introduction to sharedpreferences storage:

Sharedpreferences is a data storage method used to store simple information in a android.content packet, stored in a key-value pair mode, and a key-value pair (key-value). For basic information about storage, such as int,string,boolean,float and long.

    1. Use the Getsharedpreferences () method to get
    2. Use the Getpreferences () method to get

To implement Sharedpreferences storage steps:

First
Call the Sharedpreferences class's edit () method to get the Sharedpreferences.editor object

Second
Call the Putxxx () method of a Sharedpreferences.editor object to get the data

Third
Submitting data using the commit () method

After the introduction of file storage and sharedpreferences, the following is the SQLite database storage. But in addition to these three, there are several storage methods.

Next talk about the android in the SQLite database additions and deletions to change the operation.

SQLite database Storage

SQLite is a lightweight relational database, it is computationally fast, occupies little space, SQLite not only has SQL syntax support, independent, as well as the acid transaction of the database, SQLite database is an embedded database.

The Sqliteopenhelper class, which is the helper class for Sqlitedatabase, is used to manage the creation and upgrading of databases, Sqliteopenhelper classes are abstract classes, there are two methods to override: OnCreate () and Onupgrade (), Used to implement creating and upgrading databases.

A constructor method is also required in the Sqliteopenhelper class, which receives four parameters, namely the context context,string name, Cursorfactory factory,int version four. The first one is the parameter context, the second is the database name, the third is the custom cursor, is generally null, and the fourth is the current database version number.

There are two important instance methods in the Sqliteopenhelper class, The Getreadabledatabase () and the Getwritabledatabase () methods. Call this method to create the database. If there is a database open, it is not created.

The Sqliteopenhelper class is a SQLite helper class that implements SQLite data manipulation, creates a class to inherit Sqliteopenhelper, and follows the code example.

Next Add data

How to add data, use the put () method, provide the insert () method in Sqlitedatabase, and then explain.

Delete data

The method used is delete (), the first parameter is the table name, and the 23rd bit is used for the constraint.

Update data

The method used is the update () method, the parameters are table names, contentvalues objects, constraints, constraints, OK, the next example.

Querying data

The method used for query (), this method requires at least seven parameters (table, columns, selection, Selectionargs, groupBy, having, order by), respectively, the table name, to query the name, query condition statements, Corresponds to the value of the placeholder in the selection statement, the column name to group, the filter conditional statement after grouping, and the sort method.

Upgrade Database

Use the OnUpdate () method to delete the table separately and create it again in this method.

Code:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {    private static Integer Version = 1;   //构造函数   public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,                         int version) {       //必须调用父类当中的构造函数       super(context, name, factory, version);   }      //context:上下文对象   //name:数据库名称   //param:factory   //version:当前数据库的版本   public MySQLiteOpenHelper(Context context,String name,int version){       this(context,name,null,version);   }   public MySQLiteOpenHelper(Context context,String name){       this(context, name, Version);   }   //创建的时候被调用   @Override   public void onCreate(SQLiteDatabase db) {       //创建了数据库并创建一个的表       String sql = "create table sut(id int primary key,name varchar(200))";       db.execSQL(sql);   }   //数据库升级时调用   @Override   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {       System.out.println("更新版本为:"+newVersion);   }}
Conclusion
    • Android Storage technology knowledge points here, let's work together ~

?

Detailed Android Data storage technology

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.