Android provides many ways to store data. However, SQLite database is most commonly used in apps which require large data storage.
SQLite is a built-in relational database in Android. It is managed by classes in android.database.sqlite package. This package includes SQLite database management classes that an app can use to manage its own private database. Together with the Cursor class from the android.database package, they provide all the functionality required for performing Data Manipulation Language and query operations on an SQLite table and database.
The tutorials you need to read
1) The basics
Regarding the basic SQLite tutorial, recommend this SQLite database tutorial from http://www.tutorialspoint.com. Tutorialspoint demonstartes how to use SQLite in a Address Book. This is a basic contacts app which allows creation, update and deletion of contacts. You will get familiar with database and table creation and CRUD process.
2) Date value
An issue developer often encounter is wondering how to store date type. There are 2 methods to choose in SQLite. One is to create a column with datetime data type and store the whole date string, the other one is to convert date and time to timestamp and store it as long data type.
Regarding the 1st method, AndroidHive shared a tip about inserting datetime value here.
If you want to use timestamp, getTime() method of Date class can help convert date string to timestamp.
String date_string ="2016-06-28"; DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date = (Date) formatter.parse(date_string); System.out.println("Today is " + date.getTime());
Make it easier with Android ORM libraries
It is not ideal to keep rewriting SQLite codes when a new project comes up. That’s when Android ORM libraries come in handy. These libraries help reduce number of hours a developer has to build an app’s database from scratch. I introduces 5 most popular ORMs in my other post. It can be read here.
View SQLite Database
One needs to view SQLite’s database quite often during development process. There are many ways to access the database. I’m introducing the 2 most common ones.
1) Using Shell access
//connect to adb adb shell //switch to app's database folder. Replace package name with your real app's. cd /data/data/COM.YOUR_PACKAGE.NAME/databases //connect to the database sqlite3 DATABASE_NAME.db //execute your query Select * from TABLE_NAME where ...;
2) Using Android Device Monitor tool
In case you want something more visual, you can use Android Device Monitor tool to download the database and DB Browser for SQLite to read the db file.
Step 1. Access the tool in Android Studio: Tools > Android > Android Device Monitor.
Step 2. After the tool is launched, browse to database folder (/data/data/COM.YOUR_PACKAGE.NAME/databases) on File Explorer tab.
Step 3. Choose the database then click on the “Pull a file from the device” button.
Step 4. Download and install DB Browser for SQLite from http://sqlitebrowser.org.
Step 5. Open the database with this program.