Android数据存储小结

巧妇难为无米之催,数据是一个程序的血肉,可是Android的数据存在哪里呢?Android提供了四种方法对数据进行存储:SQLite/ContentProvider/File/SharedPreferences
因为对数据的基本操作就是增删改查,那就从这四个方面对这四种方式进行总结:


一、SQLite
用到的类:SQLiteOpenHelper/SQLiteDatabase/Cursor
SQLit支持:NULL/INTEGER/REAL/TEXT/BLOB
getReadableDatabase()
getWritableDatabase()
onCreate()
onOpen()
onUpgrade()
close()
//建库建表
Helper h = new Helper(*Activity.this,"LWDB");
SQLiteDatabase db = h.getReadableDatabase();
//库本升级
Helper h = new Helper(*Activity.this,"LWDB",2);
SQLiteDatabase db = h.getReadableDatabase();
//增
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);
//删
Helper h = new Helper(SQLiteActivity.this,"LWDB");
SQLiteDatabase db = h.getWritableDatabase();
db.delete("user", "id=?", new String[]{"1"});
db.close();
//改
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"});
//查
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"));
}
实际应用:
1、实体类
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;
}


二、ContentProvider
增:
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);
删:
Uri uri = People.CONTENT_URI;
getContentResolver().delete(uri, null, null);
改:
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, recNo);
ContentValues values = new ContentValues();
values.put(People.NAME, name);
getContentResolver().update(uri, values, null, null);
查:
String columns[] = new String[] { People.NAME, People.NUMBER };
Uri uri = People.CONTENT_URI;
Cursor cursor = managedQuery(uri, columns, null, null, null);


三、File
www.baidu.com 
key:java io


四、SharedPreferences
增:
//1、获得SharedPreferences
SharedPreferences sp = FirstDayActivity.this.getSharedPreferences("SP", Activity.MODE_PRIVATE);
//2、获得Editor
Editor editor = sp.edit();
//3、以键值对方式写入xml
editor.putString("I", "hate you");
//4、提交
editor.commit();
删:
SharedPreferences sp = FirstDayActivity.this.getSharedPreferences("SP", Activity.MODE_PRIVATE);
Editor editor = sp.edit();
editor.remove("I");
//editor.clear();
editor.commit();
改:
SharedPreferences sp = FirstDayActivity.this.getSharedPreferences("SP", Activity.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("I", "will get much money");
editor.commit();
查:
SharedPreferences sp = FirstDayActivity.this.getSharedPreferences("SP", Activity.MODE_PRIVATE);
String string = sp.getString("I", "love you");


以上只是这四种方式的基本操作,可以在此基础上写高级操作,下文再做总结。希望对你有所帮助。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。