Posts

Showing posts with the label SQLite

App data and files in Android

Image
Save data using SQLite Saving data to a database is ideal for repeating or structured data, such as contact information. Define a schema and contact Schema is a formal declaration of how the database is organized.  You should create a companion class, known as a contract class , which explicitly specifies the layout of your schema in a systematic and self-documenting way. A contract class is a container for constants that define names for URIs, tables and columns. The contract class allows you to use the same constants across all the other classes in the same package. A good way to organize a contract class is to put definitions that are global to your whole database in the root level of the class. Then create an inner class for each table. Each inner class enumerates the corresponding table's columns. For complete implementation please click the header or check the e.g. public class DeviceContract { private DeviceContract () { } public static class DeviceEntry impl...

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...