Android Training - File,SharedPreference,SQLite

数据持久化

我们日常中所说的RAM大小4G指的Temporary Storage,为我们临时的数据提供一片存储空间。为了能让数据永久的记录下来,我们就需要一个Permanent Storage来让我们的数据能够永久的保存下来。Android有三种常用的永久化数据方式:

  • File
  • Shared preference
  • SQLite

SQLite

有数据库基础的都不会陌生,SQLite用于保存大量的结构化数据,数据内容大多是文本信息,对于图片音频常用file来保存
需要注意的是SQLite不像MySQL等其他数据库需要服务器,SQLite将数据存放在本地文本里
在SQLite里存储类型非常宽泛,它为我们提供了一个Storage Class,方便我们动态使用存储类型,具体内容在这篇文章中可以找到

  • 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.比如二进制数据,图片

一些有关指令的使用:
使用sql语句操作在此略过

  • .tables-用来查看有哪些表
  • .schema 表名-用来查看表的创建
  • PRAGMA TABLE_INFO(表名)-查看表的属性
  • DROP TABLE 表名 删除表格

Shared preference

适合存储的是简短的信息,因为它的保存方式是Key-Value的形式:


image.png

顾名思义可以知道这个信息经常是帮助app记录用户的偏好设置,例如:


image.png

你可能感兴趣的:(Android Training - File,SharedPreference,SQLite)