Android Read SQLite database From Assets Folder

In this tutorial you will learn,  how can you read SQLite Database from assets folder? Sometimes you have a SQLite database file and want to read in your app. So this tutorial explain you about it.

In this example we need a .sqlite file and we put this file inside assets folder. Now let's start this example

Create activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=""

















 android:text="Hello World!" />




Add mydb.sqlite file inside assets folder.

Create model class file

package com.valueinbrief.assetsdataexample;

public class Contact {

public int id;

 public String name = "";

 public String hint = "";

public Contact(int id, String name, String hint) { = id; = name;

 this.hint = hint;


public int getId() {

 return id;


public void setId(int id) { = id;


public String getName() {

 return name;


public void setName(String name) { = name;


public String getHint() {

 return hint;


public void setHint(String hint) {

 this.hint = hint;



Create SQLite database helper class file

package com.valueinbrief.assetsdataexample;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqlLiteDbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "mydb.sqlite";
 private static final String DB_PATH_SUFFIX = "/databases/";
 static Context ctx;
public SqlLiteDbHelper(Context context) {
 super(context, DATABASE_NAME, null, DATABASE_VERSION);
 ctx = context;
public ArrayList<Contact> getDetails() {
 SQLiteDatabase db = this.getReadableDatabase();
 ArrayList<Contact> contList = new ArrayList<>();
 Cursor cursor = db.rawQuery("SELECT * FROM demo", null);
 if (cursor != null) {
 while (cursor.moveToNext()) {
Contact cont = new Contact(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
 return contList;
public void CopyDataBaseFromAsset() throws IOException {
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
 String outFileName = getDatabasePath();

// if the path doesn't exist first, create it
 File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
 if (!f.exists())

// Open the empty db as the output stream
 OutputStream myOutput = new FileOutputStream(outFileName);

// transfer bytes from the inputfile to the outputfile
 byte[] buffer = new byte[1024];
 int length;
 while ((length = > 0) {
 myOutput.write(buffer, 0, length);
// Close the streams

private static String getDatabasePath() {
 return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX
public SQLiteDatabase openDataBase() throws SQLException {
 File dbFile = ctx.getDatabasePath(DATABASE_NAME);
if (!dbFile.exists()) {
 try {
 System.out.println("Copying sucess from Assets folder");
 } catch (IOException e) {
 throw new RuntimeException("Error creating source database", e);
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
 public void onCreate(SQLiteDatabase db) {
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Create file to retrieve data from Database

package com.valueinbrief.assetsdataexample;

import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TextView readTV;
 SqlLiteDbHelper dbHelper;
 ArrayList<Contact> contList;

 protected void onCreate(Bundle savedInstanceState) {

readTV = (TextView) findViewById(;
 contList = new ArrayList<>();
 dbHelper = new SqlLiteDbHelper(this);
 contList = dbHelper.getDetails();
 String msg ="";
 for(int i=0;i<contList.size();i++){

Contact cont = contList.get(i);
 msg = msg+cont.getId()+" "+cont.getName()+" "+cont.getHint()+"\n\n\n";
If you have any question or request, mention me on twitter. Happy Coding!

If you want to get new posts, make sure to subscribe to Value In Brief by Email.