數(shù)據(jù)持久化
我們?nèi)粘V兴f的RAM大小4G指的Temporary Storage,為我們臨時的數(shù)據(jù)提供一片存儲空間。為了能讓數(shù)據(jù)永久的記錄下來,我們就需要一個Permanent Storage來讓我們的數(shù)據(jù)能夠永久的保存下來。Android有三種常用的永久化數(shù)據(jù)方式:
- File
- Shared preference
- SQLite
SQLite
有數(shù)據(jù)庫基礎(chǔ)的都不會陌生,SQLite用于保存大量的結(jié)構(gòu)化數(shù)據(jù),數(shù)據(jù)內(nèi)容大多是文本信息,對于圖片音頻常用file來保存
需要注意的是SQLite不像MySQL等其他數(shù)據(jù)庫需要服務(wù)器,SQLite將數(shù)據(jù)存放在本地文本里
在SQLite里存儲類型非常寬泛,它為我們提供了一個Storage Class,方便我們動態(tài)使用存儲類型,具體內(nèi)容在這篇文章中可以找到
- NULL. The value is a NULL value.
- INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.boolean類型以intergers0和1表示,sqlite沒有boolean類型
- REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
- TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
- BLOB. stored exactly as it was input.比如二進制數(shù)據(jù),圖片
一些有關(guān)指令的使用:
使用sql語句操作在此略過
- .tables-用來查看有哪些表
- .schema 表名-用來查看表的創(chuàng)建
- PRAGMA TABLE_INFO(表名)-查看表的屬性
- DROP TABLE 表名 刪除表格
Shared preference
適合存儲的是簡短的信息,因為它的保存方式是Key-Value的形式:

image.png
顧名思義可以知道這個信息經(jīng)常是幫助app記錄用戶的偏好設(shè)置,例如:

image.png