第六章--数据存储方案
2017-03-15 本文已影响6人
dong_hui
1.文件存储
- 将数据存储到文件中
Context类中提供一个openFileOutput方法,可存数据到文件中,接收两个参数,(文件名,文件的操作模式)
文件操作模式包括MODE_PRIVATE(表示指定同样文件名字的时候,将覆盖原文件内容),MODE_APPEND(文件存在则追加数据,文件不存在则创建数据)。
private void saveTxt(String a) {
FileOutputStream out=null;
BufferedWriter writer=null;
try {
out=openFileOutput("data", Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write(a);
}catch (Exception e){
e.printStackTrace();
}finally {
if (writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 从文件中读取数据
Context类中提供一个openFileInput方法,可从文件中读取数据,至接收一个参数(文件名字)。
private String getTxt() {
FileInputStream inputStream=null;
BufferedReader reader=null;
StringBuilder content=new StringBuilder();
try {
inputStream=openFileInput("data");
reader=new BufferedReader(new InputStreamReader(inputStream));
String line="";
while ((line=reader.readLine())!=null){
content.append(line);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
2.SharedPreferences存储
- SharedPreferences存数据
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","dh");
editor.putString("age","25");
editor.apply();
- SharedPreferences取数据
SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
String name=sharedPreferences.getString("name","");
int age=sharedPreferences.getInt("age",0);
- 案例 记住密码 功能 --看下实现逻辑
preferences= PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = preferences.getBoolean("remember_pwd",false);
if (isRemember){
String account=preferences.getString("account","");
String pwd=preferences.getString("pwd","");
username_et.setText(account);
userpwd_et.setText(pwd);
rememberpwd_cb.setChecked(true);
}
//点击事件处理
case R.id.login_btn:
String account=username_et.getText().toString();
String pwd=userpwd_et.getText().toString();
if (account.equals("11")&&pwd.equals("22")){
editor=preferences.edit();
if (rememberpwd_cb.isChecked()){
editor.putString("account",account);
editor.putString("pwd",pwd);
editor.putBoolean("remember_pwd", true);
}else {
editor.clear();
}
editor.apply();
Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();
}
break;
3.SQLite数据库存储
- 创建数据库
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table Book ("
+"id integer primary key autoincrement, "
+"author text, "
+"price real, "
+"pages integer, "
+"name text)";
private Context mcontext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mcontext=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_BOOK);
Toast.makeText(mcontext,"create database success",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
private MyDatabaseHelper myDatabaseHelper;
···
myDatabaseHelper=new MyDatabaseHelper(this,"BookStore.db",null,1);
myDatabaseHelper.getWritableDatabase(); //create database
···
- 升级数据库
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists Book");
sqLiteDatabase.execSQL("drop table if exists Category");
onCreate(sqLiteDatabase);
}
- 操作数据库
数据库的增删改查,是通过ContentValues进行的具体赋值;
4.使用开源LitePal 操作数据库
- 需补博客一篇单独完善……