Android Data Storage Summary

Source: Internet
Author: User

The Qiao woman is difficult to urge without rice, data is a program of flesh and blood, but where does Android data exist? Android provides four ways to store data: sqlite/contentprovider/file/sharedpreferences
Because the basic operation of the data is to delete and change, then from these four aspects of these four ways to summarize:


First, SQLite
Class used: Sqliteopenhelper/sqlitedatabase/cursor
Sqlit Support: Null/integer/real/text/blob
Getreadabledatabase ()
Getwritabledatabase ()
OnCreate ()
OnOpen ()
Onupgrade ()
Close ()
Build a database and build a table
Helper h = new Helper (*activity.this, "lwdb");
Sqlitedatabase db = H.getreadabledatabase ();
Library upgrade
Helper h = new Helper (*activity.this, "lwdb", 2);
Sqlitedatabase db = H.getreadabledatabase ();
Increase
Helper h = new Helper (*activity.this, "lwdb");
Sqlitedatabase db = H.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put ("id", 1);
Values.put ("name", "Liwei");
Db.insert ("User", null,values);
By deleting
Helper h = new Helper (sqliteactivity.this, "lwdb");
Sqlitedatabase db = H.getwritabledatabase ();
Db.delete ("User", "id=?", New string[]{"1"});
Db.close ();
Change
Helper h = new Helper (*activity.this, "lwdb");
Sqlitedatabase db = H.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put ("name", "Liyan");
Db.update ("User", Values, "id=", New string[]{"1"});
Check
Helper h = new Helper (*activity.this, "lwdb");
Sqlitedatabase db = H.getwritabledatabase ();
cursor cursor = db.query ("User", new string[]{"id", "name"}, "id=", New string[]{"1"}, "", "", "");
while (Cursor.movetonext ()) {
String s = cursor.getstring (Cursor.getcolumnindex ("name"));
}
Practical application:
1. Entity class
2, Sqliteopenhelper
3. Service
ADD:
Helper.getwritabledatabase (). Execsql ("INSERT into student (name, age) VALUES (?,?)", New Object[]{s.getname (), S.getage ()});
Delete:
Helper.getwritabledatabase (). Execsql ("Delete * from student where id=?", new object[] {ID});
Update:
Helper.getwritabledatabase (). Execsql ("Update student set name=?", age=? Where id=? ", New Object[]{s.getname (), S.getage (), S.getid ()});
Find
cursor cursor = db.rawquery ("SELECT * from student where id=?", New Object[]{id});
while (Cursor.movetonext ()) {
Password p = new Password ();
P.setpid (Cursor.getint (Cursor.getcolumnindex ("PId")));
P.setppassword (Cursor.getstring (Cursor.getcolumnindex ("Ppassword"));
return p;
}


Second, ContentProvider
Increase:
Contentvalues values = new Contentvalues ();
Values.put (People.name, NAME);
Uri uri = Getcontentresolver (). Insert (People.content_uri, values);
Uri Numberuri = Uri.withappendedpath (URI, People.Phones.CONTENT_DIRECTORY);
Values.clear ();
Values.put (Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
Values.put (People.number, Phoneno);
Getcontentresolver (). Insert (Numberuri, values);
By deleting:
Uri uri = People.content_uri;
Getcontentresolver (). Delete (URI, NULL, NULL);
Change:
Uri uri = Contenturis.withappendedid (People.content_uri, recNo);
Contentvalues values = new Contentvalues ();
Values.put (People.name, NAME);
Getcontentresolver (). Update (URI, values, NULL, NULL);
Check:
String columns[] = new string[] {people.name, people.number};
Uri uri = People.content_uri;
cursor cursor = managedquery (URI, columns, NULL, NULL, NULL);


Third, File
Www.baidu.com
Key:java IO


Iv. sharedpreferences
Increase:
1. Get sharedpreferences
Sharedpreferences sp = FirstDayActivity.this.getSharedPreferences ("sp", activity.mode_private);
2. Get Editor
Editor editor = Sp.edit ();
3. Write XML in key-value pair mode
Editor.putstring ("I", "hate You");
4. Submit
Editor.commit ();
By deleting:
Sharedpreferences sp = FirstDayActivity.this.getSharedPreferences ("sp", activity.mode_private);
Editor editor = Sp.edit ();
Editor.remove ("I");
Editor.clear ();
Editor.commit ();
Change:
Sharedpreferences sp = FirstDayActivity.this.getSharedPreferences ("sp", activity.mode_private);
Editor editor = Sp.edit ();
Editor.putstring ("I", "'ll get much money");
Editor.commit ();
Check:
Sharedpreferences sp = FirstDayActivity.this.getSharedPreferences ("sp", activity.mode_private);
String string = Sp.getstring ("I", "Love You");


The above is only the basic operation of these four ways, you can write advanced operations on this basis, and then summarize the following. I hope to be of some help to you.

Android Data Storage Summary

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.