Working with Android SQLite Database

Android provides many ways to store data. However, SQLite database is most commonly used in apps which require large data storage.

android_database_sqlite

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.

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.

[java]
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());
[/java]

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

[java]
//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 …;
[/java]

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.

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.

Android_Device_Monitor_database

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.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close