Skip to content

dinkar1708-zz/AndroidRoomDatabase

Repository files navigation

Saving Data Using the Room Persistence Library

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

There are 3 major components in Room:

  1. Database
  2. Entity
  3. Dao

In existing project -

Very basic usages and setup of room data base has been shown via user example.

Basic crucks-

*. Setup of data base

AppDatabase - create db

@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

appDatabase = Room.databaseBuilder(context.getApplicationContext(),
                    AppDatabase.class, "database-name").build();             

*. UserDao - interface to provide access on db-

@Dao
public interface UserDao {
    @Query("SELECT * FROM user")
    List<User> getAll();

//init service

*. Initilize service where you want to use room db-

private UserServiceImpl userService;
userService = new UserServiceImpl(LoginActivity.this);

*. From do in background save objects in db

            User user = new User();
            user.setUid(new Random().nextInt());
            user.setEmail(mEmail);
            user.setPassword(mPassword);
            user.setFirstName("First Name");
            user.setLastName("Last Name");
            userService.insertAll(user);
            users = userService.getAll();

*. Simple method to get db in background thread and showing data on UI-

 private void getUsersFromDB() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                users = userService.getAll();
                return null;
            }
            @Override
            protected void onPostExecute(Void agentsCount) {
                usersTextView.setText("Users \n\n " + users);
            }
        }.execute();

Screen shots

device-2018-01-25-202154

device-2018-01-25-202231

device-2018-01-25-202311