How to Access Sqlite Database from Assets Folder in Android Example

Please follow and like us:

How to Access Sqlite Database from Assets Folder in Android ExampleReading Sqlite database file with .sqlite extension from asset folder is very easy task with the help of Latest Android Library SQLiteAssetHelper Example. With this new Android Library we can access existing Sqlite database in our Android Application.


Loading Sqlite Database from Assets Folder in Android Application

First, add required dependencies in your app’s build.gradle file.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.sqliteassethelper.demo"
        minSdkVersion 11
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
}

Add existing sqlite database file in Assests/Databases folder like bellow image

sqlitedemo

Create MyDatabase.java file for SQLiteAssetHelper.

package com.sqliteassethelper.demo;

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

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

import java.util.ArrayList;

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "yogalite.db";
    private static final int DATABASE_VERSION = 1;
    private static final String ID="id";
    private static final String NAME="name";
    private static final String DESCRIPTION="description";
    private static final String IMAGE="image";
    private static final String BENEFITS="benefits";
    private static final String POSES_TABLE="poses";

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public ArrayList<Poses> getPoses(){
        SQLiteDatabase db=getWritableDatabase();
        String[] columns={MyDatabase.ID,MyDatabase.NAME,MyDatabase.DESCRIPTION,MyDatabase.BENEFITS,MyDatabase.IMAGE};
//        String[] selectionArgs={categoryId+"",subjectId+"",yearId+""};
        Cursor cursor=db.query(MyDatabase.POSES_TABLE, columns, null, null, null, null, null);
//        Cursor cursor=db.query(MyDatabase.TABLE_NAME, columns, null,null, null, null, null);
        ArrayList<Poses> questionsArrayList=new ArrayList<>();

        while(cursor.moveToNext()){
            Poses questions=new Poses();
            questions.id=cursor.getInt(cursor.getColumnIndex(MyDatabase.ID));
            questions.name=cursor.getString(cursor.getColumnIndex(MyDatabase.NAME));
            questions.description=cursor.getString(cursor.getColumnIndex(MyDatabase.DESCRIPTION));
            questions.benefits=cursor.getString(cursor.getColumnIndex(MyDatabase.BENEFITS));
            questions.image=cursor.getString(cursor.getColumnIndex(MyDatabase.IMAGE));
            questionsArrayList.add(questions);
        }
        return questionsArrayList;
    }



}

Create POJO class  Poses.java for DAtabase’s table Poses

package com.sqliteassethelper.demo;

public class Poses {

    public int id;

    public String name;

    public String description;

    public String benefits;

    public String image;
}

Initialize DatabaseHelper file and load data from sqlite database file to Application in MainActivity.java file

package com.sqliteassethelper.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    MyDatabase myDatabase;
    private ArrayList<Poses> posesArrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDatabase=new MyDatabase(MainActivity.this);

        posesArrayList=myDatabase.getPoses();

        Log.e("poses list",posesArrayList.size()+"");

        for(int i=0;i<posesArrayList.size();i++){
            Log.e(" category filter",posesArrayList.get(i).name+"");
        }


    }
}

Add write external storage permission in your AndroidMainifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sqliteassethelper.demo">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Final Words

Note: This demo will not work in Android 6.0. If you want it for Android 6.0 then you have to add Android Runtime permissions.
Hope you like our simple tutorial of loading sqlite database file from asset folder in Android Application.

nirav kalola