Posts

Showing posts with the label Database

SQLite in Android

Image
Overview SQLite is an opensource SQL database that is used to store data into a text file on a device. SQLite only supports NULL INTEGER REAL TEXT BLOB SQLite with MVVM Add the required dependency in build.gradle (Module) file. Create a data class data class User( val name : String , val dob : String , val address : String , val pin : String , val username : String , val password : String , val isLoggedIn : Int ) Create a class name DBHelper and extend it with SQLiteOpenHelper Create the repositories Some Syntax of SQL commands Do's and Dont's You cannot store a BOOLEAN value in SQLite, you have to use INTEGER for this purpose. Code to Open Database from external file public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "appDatabase.db" ; private static final int DATABASE_VERSION = 1 ; private static String DATABASE_PATH ; private final Context context ; private SQLiteDat...

Room Database in Android

Image
Overview Room database is a persistence library of Android Jetpack. It is used to deal with database operations efficiently. It provides and abstraction layer over SQLite. Steps to follow Dependencies def room_version = "2.4.0" implementation "androidx.room:room-runtime: $room_version " kapt "androidx.room:room-compiler: $room_version " implementation "androidx.room:room-ktx: $room_version " annotationProcessor "androidx.room:room-compiler: $room_version " def coroutine_version = "1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $coroutine_version " implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android: $coroutine_version " def lifecycle_version = "2.4.0" implementation "androidx.lifecycle:lifecycle-livedata-ktx: $lifecycle_version " implementation "androidx.lifecycle:lifecycle-viewmodel-ktx: $lifecycle_version " Updated Dependencies ...

MVVM Design Pattern in Android

Image
Overview MVVM is a Model-View-ViewModel design pattern architecture that removes the tight coupling between every application components. In the below image, you can clearly see that every children components don't have a reference to the parent components. They have a reference to an observable though which they can communicate with the parent components. Components of MVVM Model It is the data and business logic of an Android application. It's a data class where you can define the properties and methods too. View It is the user interface of an Android application, activities and fragments. These views send the user requests to ViewModel and to get the response, the application components has to subscribe to the observables which the ViewModel has exposed to it. ViewModel It is a bridge between the View and the Model in MVVM design pattern. ViewModel isn't aware which view is interacting with it. ViewModel only interacts with the Model and exposes the observable that can b...