Android學習筆記—第八章 數據存儲

第八章 數據存儲

  1. 數據存儲方式

    Internal Storage 內部存儲

    External Storage 外部存儲

    SQLite DataBase 數據庫存儲

    Http 網絡存儲

  2. Shared Prefrences 參數共享

    存儲位置:data/data/包名/shared_prefs/MainAcitivy.xml

    格式:xml

    保存數據:

    //獲取Shared Prefrences類型對象

    SharedPrefrences sp = getSharedPrefrences("xxx",0);

    或者

    SharedPrefrences sp = getPrefrences(0);

    //獲取Edit類型的對象

    Edit edit = sp.edit();

    //寫入數據

    edit.putString("name","小明");

    edit.putBoolean("isTrue","true");

    //提交數據(必須要有)

    edit.commit();



    提取數據

    //獲取SharedPrefrences類型對象

    SharedPrefrences sp = getSharedPrefrences("xxx",0);

    或者

    SharedPrefrences sp = getPrefrences(0);

    //根據存入的key值取出相應的數據

    String name = sp.getString("name",小紅);

    boolean b = sp.getBoolean("isTrue",false);

  3. Internal Storage 內部存儲

    存儲位置:data/data/包名/files/xxx.xxx

    格式:自定義

    保存數據:

    FileOutputStream fos = openFileOutput("aaa",0);

    String str = "數據存儲";

    fos.write(str.getBytes());

     

    讀取數據

    FileInputStream fis = openFileInput("aaa");

    ByteArrayBuffer arrayBuffer = new ByteArrayBuffer(100);

    byte[] b = new byte[1024];

    int len = 0;

    while(-1!=(len=fis.read(b))){

        arrayBuffer.append(b,0,len);

    }

    String str = new String(arrayBuffer.toByteArray());

  4. External Storage 外部存儲

    存儲位置:mnt/sdcard

    讀取權限:

    (1)sdcard路徑:

    Environment.getExternalStorageDirectory().getAbsolutePath();

    (2)判斷sdcard是否可用:

    Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());

  5. SQLite DataBase 數據庫存儲

    Android平臺集成小型數據庫SQLite

    特點:跨平臺、免費、輕量級、多線程

    SQL語句:

    與表相關:

    a. 建表:CREATE TABLE 表名 (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))

    b. 刪表:DROP TABLE 表名

    c. 改表:ALERT TABLE 表名 ADD age INTEGER

    與數據相關:增、刪、改、查

    a. 增加數據:INSERT INTO 表名 (字段名) VALUES (值)

    b. 刪除數據:DELETE FROM 表名 WHERE 條件

    c. 修改數據:UPDATE 表名 SET 字段名=值 WHERE 條件

    d. 查詢數據:SELECT * FROM 表名 WHERE 條件 GROUP BY 分組 ORDER BY 排序

    創建SQLite數據庫的步驟:

    a. 創建一個輔助類繼承SQLiteOpenHelper

    b. 在輔助類的onCreate()方法中,添加創建表的語句,執行該語句

    String str = "CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER, phone VARCHAR(20))";

    db.execSQL(str);

    c. 在Activity中,先創建輔助類對象,再用輔助類對象調用getReadableDatabase()或者getWriteableDataBase()創建數據庫對象

    備註:當數據庫和表都已經存在時,onCreate()方法不會再調用

          當version發生變化時,會調用onUpgrade()方法

    SQL語句實例:

    (1)增

    String str = "INSERT INTO student (name,phone) VALUES (?,?)";

    mDB.execSQL(str,new String[]{"小明","1111"});

    (2)刪

    String str = "DELETE FROM student WHERE name = ?";

    mDB.execSQL(str,new String[]{"小明"});

    (3)改

    String str = "UPDATE student SET name = ?,phone = ? WHERE id = ?";

    mDB.execSQL(str,new String[]{"小紅","2222","1"});

    (4)查

    String str = "SELECT * FROM student";

    Cursor cursor = mDB.rawQuery(str,null);

    while(cursor.moveToNext()){

        //獲取各字段值的索引

        int idIndex = cursor.getColumnIndex("id");

        int nameIndex = cursor.getColumnIndex("name");

        int phoneIndex = cursor.getColumnIndex("phone");

        //根據索引取出數據

        String id = cursor.getString(idIndex);

        String name = cursor.getString(nameIndex);

        String phone = cursor.getString(phoneIndex);

    }

    ORM 對象關係映射

    (1)增

    ContentValues values = new ContentsValues();

    values.put("name","小白");

    values.put("phone","12356");

    mDB.insert("student", //表名

                null;//規避插入錯誤

                values);//插入的值

    (2)刪

    mDB.delete("student",//表名

                "name=?",條件語句

                new String[]{"小白"});佔位符的填充

    (3)改

    ContentValues values = new ContentValues();

    values.put("phone","45621");

    mDB.update("student",//表名

                values,//將要修改的值

                "name=?",條件語句

                 new String[]{"小白"});//佔位符填充

    (4)查

    Cursor cursor = mDB.query("student",//表名

                               null,//查詢的字段

                               null,//查詢條件

                               null,//條件語句佔位符的填充

                               null,//分組語句

                               null,//分組語句佔位符的填充

                               null);//排序語句

    while(cursor.moveToNext()){

        //獲取字段索引

        int nameIndex = cursor.getColumnIndex("name");

        //根據索引取出數據

        String name = cursor.getString(nameIndex);

    }

  6. CursorAdapter遊標適配器

    注意:必須含有_id

    // 將佈局xml文件轉換成佈局對象

      @Override

      public View newView(Context context, Cursor cursor, ViewGroup parent) {

       View inflate = getLayoutInflater().inflate(R.layout.list_item, null);

       return inflate;

      }

      // 設置數據顯示

      @Override

      public void bindView(View view, Context context, Cursor cursor) {

       TextView textView1 = (TextView) view.findViewById(R.id.textView1);

       TextView textView2 = (TextView) view.findViewById(R.id.textView2);

       // 獲取數據

       String name = cursor.getString(cursor.getColumnIndex("name"));

       String phone = cursor.getString(cursor.getColumnIndex("phone"));

       // 設置屬性值

       textView1.setText(name);

       textView2.setText(phone);

      }

    更新數據

      // 重新查詢,得到一個新的cursor對象

      Cursor cursor = mDB.query("student", null, null, null, null, null, null);

      // 將最新的cursor對象設置到適配器中

      mAdapter.changeCursor(cursor);

      // 刷新顯示

      mAdapter.notifyDataSetChanged();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章