第六章--数据存储方案

2017-03-15  本文已影响6人  dong_hui

1.文件存储

    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();
                 }
             }
        }

    }

 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.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
        editor.putString("name","dh");
        editor.putString("age","25");
        editor.apply();
        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);
    }

4.使用开源LitePal 操作数据库

上一篇下一篇

猜你喜欢

热点阅读