Code Monkey home page Code Monkey logo

androidroomdatabase's Introduction

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

androidroomdatabase's People

Contributors

dinkar1708 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.