Home / Data Storage / Import SQLite Database into Android using SQLite Browser
Import SQLite Database into Android using SQLite Browser

Import SQLite Database into Android using SQLite Browser

How to Import SQLite Database into Android using SQLite Browser, Most all of the Android tutorials assume that you want to create and populate your database at runtime by Java coding but what if we need to access an independent, preloaded database with your Android application.

In our previous tutorial we talked about Android Design SQLite Database using Workbench and generated SQLite file that we will use in this tutorial.

In this tutorial we are going to take the SQLite database file, import it into SQLite Browser (Download From Here) to generate our database and insert some content into it so it will be preloaded, then we’ll copy this file into the assets then we would access it via Java code to deal with it as a normal database.

What is SQLite Browser ?

DB Browser for SQLite is a high quality, visual, open source tool to create, design, and edit database files compatible with SQLite.

It is for users and developers needing to create databases, search, and edit data. It uses a familiar interface, so you don’t need to learn complicated SQL commands.

Database Overview:

Our database contains 3 tables, Employee which has some attributes like: Name, Age, Mail, Phone and the other for Department which has attributes like: Title.

The Employee table has multi-valued attribute which is Phone, so we create a table called EmployeePhone and link between by putting a reference for EmployeeID in EmployePhone table.

Screenshot of the Import SQLite Database into Android using SQLite Browser:

Download Source Code

output

Import SQLite Database into Android using SQLite Browser

1. Creating our SQLite database file, you can just create and new text file and make its extension to be .sql and paste this code into it or follow the steps to generate SQLite database file using Workbench like the previous tutorial.

 

2. Open SQLite Browser, click file => import => Database from SQL File, just give the path of the SQLite database file, then choose a path to save you generated database file and give it a name, in my case i will call my database (employeedb).

Now we can move to the Android part and import SQLite Database into Android using SQLite Browser, but we first need add some data in this database we can retrieve it in out Android app.

So in SQLite Browser, click on Browse Data, choose the table we need to insert data into it and click New Record.

Import SQLite Database into Android using SQLite Browser

We will add just three employees with their mobile numbers and they are in just two departments. So the final database tables will look like:

Import SQLite Database into Android using SQLite Browser

Click save and lets deal with this preloaded SQLite database in Android.

3. Creating Android Project, In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

4. In your android project folders under app\src\main make a new folder call it assets and move the database file which was generated from SQLite Browser.

Import SQLite Database into Android using SQLite Browser

5. Let’s add some styles, Open colors.xml under res ⇒ values and add below color resources. If you don’t find colors.xml, create a new file with the name.

And Open styles.xml under res ⇒ values and add below color resources.

 

6. Now we need to create a class which will handle database operations like Open, Close, Create, and CRUD operations, so under helper package, create a java class named EmployeeDBHelper.java and add below code.

This class has these following steps:

  • Determine the path for the database file in the assets folder
  • Method for opening the database and close it.
  • Two main methods to retrieve data from Employee and Department tables.
 

7. Under data package, create a java class named Employee.java and add below code. This is the model class which represents the employee object, so when we retrieve the data from Employee table we put it into Employee Object so we can deal with it easily in the app.

 

8. Under data package, create a java class named Department.java and add below code. This is the model class which represents the department object, so when we retrieve the data from Department table we put it into Department Object so we can deal with it easily in the app.

 

9. For the Main Activity layout, add the following code to activity_main.xml.

10. Finally in the Java part of the MainActivity we add the following code to get all data from database and just Toast it.

11. Run the project and test it. You should able to Import SQLite Database into Android using SQLite Browser as seen in the screenshot of the final product.

In the Next tutorial we will publish the retrieved data into Recyclerview and this tutorial helps to understand how the Recyclerview works.

Download Source Code

Summary
Article Title
Import SQLite Database into Android using SQLite Browser
Description
Import SQLite Database into Android using SQLite Browser, import preloaded database into your Android application, you don't need to learn hard SQL commands
Author
Organization
Android Gifts

About #AndroidGifts

#AndroidGifts

Leave a Reply