使用 Room Database 制作 Notes 应用程序
介绍
数据持久性是大多数应用程序的基本要求之一。SQLite 是一个开源库,是 Android 应用程序持久化数据的一种方式。然而,SQLite 的实现需要大量的样板代码。这有缺点:
- 查询中的语法错误
- 没有编译时错误检测(耗时)
- 需要进行解析才能将数据转换为普通旧式 Java 对象 (POJO)对象
这些问题在问答论坛中相当常见,这可能是 Realm、GreenDAO 和 Room 等流行的 No-SQL 数据库出现的原因。Room 是一个持久库,它使用注释抽象出大部分 SQLite 代码。
** 本教程旨在涵盖:**
- Android 的存储选项
- SQL 与 No-SQL
- 房间库基础知识
- 使用 Room Library 构建记事本应用程序
Android存储机制介绍
核心数据存储机制
- 键值对:SharedPreferences,一种 Android 框架 API,它将键值对存储在受保护的文件系统下的 XML 文件中。
- 通过 SharedPreference 存储的数据只能在应用程序内访问。
- 只能存储布尔值、整数、长整数、字符串和字符串集合。
SQL 与 NoSQL
特征 | SQL | 无SQL |
---|---|---|
存储的数据 | 表格形式(RDBMS) | POJO 对象或文档 |
数据处理 | 通过 DML、DDL | 通过提供的 API |
结构 | 关系数据库管理系统 | 键值对 |
架构 | 固定架构 | 动态,可以随时添加记录 |
可扩展 | 关系数据库管理系统 | 键值对 |
Android 支持 | SQLite | Room(半 SQL)、GreenDAO、Realm |
房间基本信息
Room 库充当底层 SQLite 数据库的抽象层。因此,Room 注释用于:
- 对于数据库和实体,其中实体是表示表结构的 POJO 类。
- 指定检索、更新和删除的操作。
- 添加约束,例如外键。
- 支持 LiveData。
Room 中有 3 个主要组件
- 实体:使用@Entity注释注释的类将映射到数据库中的表。每个实体都保存在其自己的表中,类中的每个字段代表列名。
- tableName属性用于定义表的名称
- 每个实体类必须至少有一个主键字段,用@PrimaryKey注释
- 实体类中的字段可以使用@ColumnInfo(name = “name_of_column”)注释来指定特定的列名
房间结构
构建记事本应用程序
希望您现在有足够的兴趣尝试 Room 库!我们将构建一个 Notes 应用程序,让用户可以执行以下操作:
- 在数据库中创建并保存注释
- 显示笔记列表
- 更新和删除笔记
我们在此创建的演示应用程序有望展示面向数据库的应用程序技能并帮助您开始使用 Room。但从概念上讲,此代码可以进一步扩展或更改以构建警报应用程序、调度应用程序、SMS 驱动的应用程序等。
所有代码均可在Github Repo获取
入门
Create a new project in Android Studio. Choose "Basic Activity" template.
Add a Maven Repository
Next, add a Maven Repository. Open build.gradle project and add following Maven dependency
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Google Maven Repository provides all the updated support libraries, which will be downloaded by gradle instead of downloading from SDK Manager.
Then we will add a Room dependency in build.gradle(Module:app) within the dependency block.
dependencies {
//... other dependencies
// Room dependencies
compile 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
}
Create Entity
Before creating a database, Let's create an Entity, named as Note and later, Objects of this class will be added to database.
To do this:
- Create a class named Note.
- Add @Entity annotation on the class.
- Add ID, content, and title fields.
- Important: mark at least one field with @PrimaryKey annotation.
- Use alt+insert to implement constructor, override getter and setter, and optionally override equals or toString.
package com.example.pavneet_singh.roomdemo.notedb.model;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
@Entity
public class Note {
@PrimaryKey(autoGenerate = true)
private int note_id;
@ColumnInfo(name = "note_content") // column name will be "note_content" instead of "content" in table
private String content;
private String title;
private
public Note(int note_id, String content, String title) {
this.note_id = note_id;
this.content = content;
this.title = title;
}
public int getNote_id() {
return note_id;
}
public void setNote_id(int note_id) {
this.note_id = note_id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
</fon
免责声明:本内容来源于第三方作者授权、网友推荐或互联网整理,旨在为广大用户提供学习与参考之用。所有文本和图片版权归原创网站或作者本人所有,其观点并不代表本站立场。如有任何版权侵犯或转载不当之情况,请与我们取得联系,我们将尽快进行相关处理与修改。感谢您的理解与支持!
请先 登录后发表评论 ~