数据库持久性_房间持久性库入门指南

数据库持久性

by Ajin Augustine

通过Ajin Augustine

房间持久性库入门指南 (A Beginner’s Guide to the Room Persistence Library)

It’s not a difficult task for an Android developer to convert raw data into a structured database for internal storage. This is done using the most reliable language — SQL. The inbuilt SQLite core library is within the Android OS. It will handle CRUD (Create, Read, Update and Delete) operations required for a database. Java classes and interfaces for SQLite are provided by the android.database. SQLite maintains an effective database management system. But this conventional method has its own disadvantages.

对于Android开发人员而言,将原始数据转换为用于内部存储的结构化数据库并非难事。 这是使用最可靠的语言-SQL完成的。 内置SQLite核心库位于Android OS中。 它将处理数据库所需的CRUD(创建,读取,更新和删除)操作。 android.database提供了SQLite的Java类和接口。 SQLite维护有效的数据库管理系统。 但是这种常规方法有其自身的缺点。

  • You have to write long repetitive code, which will be time-consuming as well as prone to mistakes.

    您必须编写冗长的重复代码,这既耗时又容易出错。
  • It is very difficult to manage SQL queries for a complex relational database.

    管理复杂的关系数据库SQL查询非常困难。

To overcome this, Google has introduced Room Persistence Library. This acts as an abstraction layer for the existing SQLite APIs. All the required packages, parameters, methods, and variables are imported into an Android project by using simple annotations.

为了克服这个问题,Google引入了Room Persistence Library。 这充当现有SQLite API的抽象层。 使用简单的批注将所有必需的包,参数,方法和变量导入到Android项目中。

Let’s have a look at how to implement this with an example.

让我们看一下如何通过示例来实现它。

1. Add the gradle dependencies in the build.gradle file.

1.将gradle依赖项添加到build.gradle文件中。

implementation “android.arch.persistence.room:runtime:1.0.0”annotationProcessor “android.arch.persistence.room:compiler:1.0.0”

2. Create a data model class for the database table and annotate its table name and primary key.

2.为数据库表创建一个数据模型类,并注释其表名和主键。

@Entity public class Movies { @NonNull @PrimaryKey private String movieId; private String movieName;  public Movies() { }  public String getMovieId() { return movieId; } public void setMovieId(String movieId) { this.movieId = movieId; } public String getMovieName() { return movieName; } public void setMovieName (String movieName) { this.movieName = movieName; } }

3. Create an interface class for Database access. Create abstract methods for CRUD operations. Add custom SQL query as a method.

3.创建用于数据库访问的接口类。 创建CRUD操作的抽象方法。 添加自定义SQL查询作为方法。

@Dao public interface DaoAccess {  @Insert void insertOnlySingleMovie (Movies movies); @Insert void insertMultipleMovies (List<Movies> moviesList); @Query (“SELECT * FROM Movies WHERE movieId = :movieId“) Movies fetchOneMoviesbyMovieId (int movieId); @Update void updateMovie (Movies movies); @Delete void deleteMovie (Movies movies); }

4. Create a Database class for database implementation.

4.创建用于数据库实现的数据库类。

@Database (entities = {Movies.class}, version = 1, exportSchema = false) public abstract class MovieDatabase extends RoomDatabase { public abstract DaoAccess daoAccess() ; }

5.Declare and initialize an object for the Database class in your Activity or Fragment class.

5,在Activity或Fragment类中为Database类声明并初始化一个对象。

private static final String DATABASE_NAME = “movies_db”; private MovieDatabase movieDatabase; movieDatabase = Room.databaseBuilder(getApplicationContext(), MovieDatabase.class, DATABASE_NAME) .fallbackToDesctructiveMigration() .build();

The initial steps are done. By using the database object, you can do all functionalities for database management.

初始步骤已完成。 通过使用数据库对象,您可以执行数据库管理的所有功能。

Sample Insert code:

样本插入代码:

new Thread(new Runnable() { @Override public void run() { Movies movie =new Movies(); movie.setMovieId( “2”); movie.setMovieName(“The Prestige”); movieDatabase.daoAccess () . insertOnlySingleMovie (movie); } }) .start();

Always use a Thread, AsyncTask, or any worker threads to perform database operations.

始终使用Thread,AsyncTask或任何辅助线程来执行数据库操作。

For further information, please check out:

有关更多信息,请查看:

https://developer.android.com/training/data-storage/room/index.html

https://developer.android.com/training/data-storage/room/index.html

Experience seamless coding now that there’s ROOM for improvement!

现在有了ROOM可以改进的地方,体验无缝编码!

Originally published at thinkpalm.com.

最初发布在thinkpalm.com上

翻译自: https://www.freecodecamp.org/news/room-sqlite-beginner-tutorial-2e725e47bfab/

数据库持久性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值