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 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.
Table of Contents
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 demonstrates how to use SQLite in an Address Book. This is a basic contacts app that allows the creation, update, and deletion of contacts. You will get familiar with database and table creation and the CRUD process.
2) Date value
Issue developers often encounter is wondering how to store data type. There are 2 methods to choose from 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 the Date class can help convert the date string to a 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 the number of hours a developer has to build an app’s database from scratch. I introduce 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 the 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
//switch to app’s database folder. Replace package name with your real app’s.
//connect to the database
//execute your query
Select * from TABLE_NAME where …;
2) Using Android Device Monitor tool
In case you want something more visual, you can use the 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 the 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.