Android Development — Part 6
Storage Options
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences:-The storage that allows you to save and retrieve persistent key-value pairs of primitive data types. SharedPreference class is general framework to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed). For example the setting that are saved in android devices like keeping mobile in slient or adjusting brightness are shared preferences.
Internal Storage:- It is a private data store on the device memory.It is storage for bigger files like pictures, videos etc.For example storing office video from YouTube is an internal storage.
External Storage:- Every Android-compatible device supports a shared “external storage” that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.
SQLite Databases:- A database is organized collection of related and structured data for easy access in term of rows and columns in tables. Row represents each objects and columns represent attribute of database . SQLite database are specific type of database. It is commonly used to store text. SQLite is a library to store local database on the device that our app is interacting with. Lite part refers to light version of database.
Installing SQLite in computer Windows.
- Download sqlite-tools-win32-*.zip Zip files.
- Create a folder called C:\sqlite to extract them into.
- Add C:\sqlite to your PATH environment variable.
- Run the sqlite3 command on your command prompt.
Some command used in SQLite (after step 4)
.open text.db => to open text.db and if not create it creates on and opens it
.help => shows cmd used in SQLite
.tables => shows tables of database
SELECT <columns> FROM<table-name>; => shows selected columns of table. Example to select all from table:- SELECT * FROM test;
CREATE TABLE <table_name> (<column_name1>,<column_name2>,….); => create table in database Example to create table students CREATE TABLE students (name TEXT, id INTEGER, section STRING);
Here TEXT, INTEGER,STRING are data types i.e it defines what the field is going to be about. For example INTEGER we can store any whole number.
INSERT INTO <table_name> (<column_name1>,<column_name2>,…) VALUES (<value1>,<value2>,…); => Insert values in columns in table.For Example INSERT INTO students (name, id, class) VALUES (“aaa”, 1, “, “A”);
UPDATE <table_name> SET <column_name> = <value> WHERE <column_name1> = <value1> ; => Updates value of column in table.For Example UPDATE students SET name = “bbb” WHERE id ==1;
Update name to “bbb” whose id is 1.
DELETE FROM <table_name>; => Deletes all from table. FOR Example DELETE FROM students;
These are queries used for CRUD (Create, Read, Update,Delete) operation.