Use Existing SQLite Database in Android App
In my previous post about SQLite , I only post the way to create database when application started.
In reality, we do not only need a database to store data but also need one to read information from.
So, in this post, I will show you how to use existing SQLite database in Android app.
The basic idea for this case is instead of creating a new database, we copy the exist one to the application’s data folder.
Very simple, right?
Now, we’ll go straight to the demo for easier understanding.
First, create a Test database:
Enter some dummy data:
The very first thing you need to do when you want to use SQLite is create a sub class of SQLiteOpenHelper.
Create some constants for database name, table name, and the database path:
Note that ice.tea09.splitedemo is your project namespace.
Implement constructor, and some methods of super class:
Method checkDatabase() allow you to check if the database exists on the phone or not:
Method copyDatabase() used to copy SQLite file in assets folder to data folder of application on the phone:
Finally, createDatabase() method - which will be called every time we launch app. It will check if the database does not exist on the phone, It’ll copy the database to data folder of application:
Finally, method that help you retrieve data from database:
The way we using DatabaseHelper in MainActivity:
Feel free to share!