android数据存储: 4种方式
1. SharedPreferences
底层使用xml文件保存数据, 优点是操作简单,结构简单
缺点是不适宜保存一些复杂,海量数据
2. 文件 + IO
优点: 有java基础就很方便学习
缺点: 操作,检索,更新不方便
3. SQLite android内部数据库
4. 内容提供器
搭建数据共享的桥梁
SharedPreferences存储数据:
1)保存数据:
/*public SharedPreferences getSharedPreferences(String name,int mode)
* 参数:
name - 声明xml文件的名字,如果该文件不存在,在 SharedPreferences.edit(),Editor.commit()后文件被创建.
mode - 对目标xml文件操作的权限.选值有:
1)0 或者Context.MODE_PRIVATE是默认操作,表示只有创建文件的程序对该目标可读可写
2)Context.MODE_WORLD_READABLE:全局读,除了创建文件的程序对该目标可读可写之外,其它程序可以对该文件进行读操作
3)Context.MODE_WORLD_WRITEABLE:全局写,除了创建文件的程序对该目标可读可写之外,其它程序可以对该文件进行写操作
返回:
Returns the single SharedPreferences instance that can be used to retrieve and modify the preference values.
另请参见:
*/
SharedPreferences sp = this.getSharedPreferences("ACCOUNT", Activity.MODE_PRIVATE);
// 如果是要完成写操作, 那么必须先创建 编辑器对象
Editor edit = sp.edit();
// 完成信息的编辑
edit.putString("user","admin");
edit.putString("passwd","123456");
// 提交数据
edit.commit();
2)取出数据:
/*
public abstract String getString (String key, String defValue)
Parameters
key The name of the preference to retrieve.
defValue Value to return if this preference does not exist
*/
SharedPreferences sp = this.getSharedPreferences("ACCOUNT", Activity.MODE_PRIVATE);
// 如果是要完成写操作, 那么必须先创建 编辑器对象
Editor edit = sp.edit();
String name = sp.getString("user","default_name");
String passwd = sp.getString("passwd","default_password");
清除数据:
Editor edit = sp.edit();
edit.clear()
edit.summit();
JavaSE IO数据存储:
IO分类:
1)传输方向:输入流和输出流
2)传输单位:字节流和字符流
3)传输功能:节点流和过滤流
输入字节流根父类:InputStream
FileInputStream,DataInputStream,BufferedInputStream,ObjectInputStream
输出字节流根父类:OutputStream
FileOutputStream,DataOutputStream,BufferedOutputStream,ObjectOutputStream
输入字符流根父类:Reader
InputStreamReader,BufferedReader
输出字符流根父类:Writer
OutputStreamWriter,BufferedWriter,PrintWriter
class Person implements java.io.Serializable{
}
class User extends Person {
private transient Computer computer = new Computer();
private List<Computer> list = new ArrayList<Computer>();
public User(){
list.add(new Computer());
}
}
class Computer// implements java.io.Serializable
{
}
序列化对象:
1)要求对象类型要实现java.io.Serializable
2)要求对象类型中的所有属性类型要实现java.io.Serializable,否则该属性用transient
3)如何有集合属性,要求加到该集合属性中的对象类型也要实现java.io.Serializable
4)如果父类没有实现java.io.Serializable,则父类对象不会序列化到硬盘,在反序列的时候,父类需要重新构造一个对象;
如果父类实现java.io.Serializable,则父类对象也会序列到硬盘,在反序列的时候,父类不需要重新构造
1)保存数据:
FileOutputStream fos = this.openFileOutput("content.txt", Context.MODE_APPEND);
String content = etContent.getText().toString();
fos.write(content.getBytes());
2)读取数据:
FileInputStream fis = this.openFileInput("content.txt");
byte[] buf = new byte[100];
int len = 0;
StringBuffer sb = new StringBuffer();
String s = null;
while((len = fis.read(buf))!=-1){
s = new String(buf,0,len);
sb.append(s);
};
SQLite步骤:
1)创建一个SQLiteDatabase对象
SQLiteDatabase sqlite = this.openOrCreateDatabase("gjun",Context.MODE_PRIVATE, null);
2)执行SQL语句:
String sql = "create table users(id integer primary key,name varchar(20)," +
"password varchar(20),email varchar(20),phone varchar(20))";
sqlite.execSQL(sql);
A、插入操作:
//插入数据第一种方式:
/*String sql = "insert into users(id,name,password,email,phone) values(?,?,?,?,?)";
sqlite.execSQL(sql, new Object[]{id++,"stone","123","stone@163.com","13631363270"});
//插入数据第二种方式:
ContentValues cv = new ContentValues();
cv.put("id", id++);
cv.put("name", "Lily");
cv.put("password", "123");
cv.put("email", "Lily@163.com");
cv.put("phone", "13631363280");
sqlite.insert("users", null, cv);
B、删除或者更新操作:
String sql = "delete from users";
sqlite.execSQL(sql);
C、查询操作:
String sql = "select * from users";
Cursor cursor = sqlite.rawQuery(sql,null);
while(cursor.moveToNext()){
System.out.println(cursor.getInt(0)+"\t"+cursor.getString(1)+"\n");
}
cursor.close();
3)关闭数据库:
public void onDestroy() {
String sql = "drop table users";
sqlite.execSQL(sql);
System.out.println("*********drop table successfully*********");
//关闭数据库
sqlite.close();
//删除数据库
this.deleteDatabase("gjun");
}
使用SQLIteOpenHelper:
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context,name,factory,version);
}
//当数据库第一次生成的时候被调用
public void onCreate(SQLiteDatabase db) {
System.out.println("*********DBHelper's onCreate(SQLiteDatabase db)*********");
String sql = "create table users(id integer primary key,name varchar(20)," +
"password varchar(20),email varchar(20),phone varchar(20))";
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class SQLiteView extends Activity implements OnClickListener {
private SQLiteDatabase sqlite = null;
private DBHelper dbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.sqliteview);
dbHelper = new DBHelper(this,"gjun",null,1);
sqlite = dbHelper.getReadableDatabase();
sqlite.execSQL(sql);
System.out.println("*********create table successfully*********");*/
}
}
使用终端查看sqlite步骤:
D:\stone\course\3G\android-sdk-windows\tools>adb shell
# cd data/data
cd data/data
# cd demo.abc
# cd databases
# sqlite3 gjun
sqlite> .table