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