Android學習筆記—第八章 資料存放區

來源:互聯網
上載者:User

標籤:資料庫   資料存放區   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();

Android學習筆記—第八章 資料存放區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.