Android Sixth day

Source: Internet
Author: User

Data storage, persistence technology.

Android offers four storage technologies: File storage, Sharedpreference, database storage, (SD card, not used).

File storage

Writing data to a file: Key code

out = openfileoutput("Data", context.mode_private);

writer = new BufferedWriter (new OutputStreamWriter (out));

Writer.write (Inputtext);

To see if the file was written successfully: DDMS view,/data/data/com.example.file/files/below.

Reading data from a file: Key code

Openfileinput ("Data");
reader = new BufferedReader (new InputStreamReader (in));
while (line = Reader.readline ()) = null) {
Content.append (line);
}

Sharedpreference

To use Sharedpreference to store data , first get to Sharedpreference object, three ways to get the object.

Getsharedpreference methods in the 1.Context class:

Context.getsharedpreference (file name, operating mode);

Getpreference methods in the 2.Activity class

Getdefaultsharedpreference methods in the 3.PreferenceManager class

Once the object has been obtained, the data is stored in the Sharedpreference file, which is divided into three steps:

1. Call the edit () method of the Sharedpreference object to get a Sharedpreference.editor object.

2. Add data to the Sharedpreference object, for example, putstring ().

3. Invoke commit () commit.

reading Data

Sharedpreferences pref = getsharedpreferences ("Data", mode_private);
String name = pref.getstring ("name", "");
int age = Pref.getint ("Age", 0);

SQLite database Storage
By using the Sqliteopenhelper class to create and upgrade a database, it is an abstract class that must create a class on its own to inherit it, and rewrite its two methods: OnCreate and Onupgrade
DBHelper = new Mydatabasehelper (This, "bookstore.db", null,1);
Dbhelper.getwritabledatabase ();

For the Onupgrade method to execute, DBHelper = new Mydatabasehelper (This, "bookstore.db", null,1); Here the fourth parameter as long as the incoming is larger than 1 can.
Add Data:

Update data:

Delete data:

Query data:

To manipulate the database using sql:

Using transactions:

Android Sixth day

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.