Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:26:36+00:00 2026-05-29T07:26:36+00:00

I have already created an SQLite database. I want to use this database file

  • 0

I have already created an SQLite database. I want to use this database file with my Android project. I want to bundle this database with my application.

Instead of creating a new database, how can the application gain access to this database and use it as its database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:26:36+00:00Added an answer on May 29, 2026 at 7:26 am

    NOTE:
    Before trying this code, please find this line in the below code:

    private static String DB_NAME ="YourDbName"; // Database name
    

    DB_NAME here is the name of your database. It is assumed that you have a copy of the database in the assets folder, so for example, if your database name is ordersDB, then the value of DB_NAME will be ordersDB,

    private static String DB_NAME ="ordersDB";
    

    Keep the database in assets folder and then follow the below:

    DataHelper class:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.content.Context;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    
    public class DataBaseHelper extends SQLiteOpenHelper {
    
        private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
        private static String DB_NAME ="YourDbName"; // Database name
        private static int DB_VERSION = 1; // Database version
        private final File DB_FILE;
        private SQLiteDatabase mDataBase;
        private final Context mContext;
    
        public DataBaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            DB_FILE = context.getDatabasePath(DB_NAME);
            this.mContext = context;
        }
    
        public void createDataBase() throws IOException {
            // If the database does not exist, copy it from the assets.
            boolean mDataBaseExist = checkDataBase();
            if(!mDataBaseExist) {
                this.getReadableDatabase();
                this.close();
                try {
                    // Copy the database from assests
                    copyDataBase();
                    Log.e(TAG, "createDatabase database created");
                } catch (IOException mIOException) {
                    throw new Error("ErrorCopyingDataBase");
                }
            }
        }
    
        // Check that the database file exists in databases folder
        private boolean checkDataBase() {
            return DB_FILE.exists();
        }
    
        // Copy the database from assets
        private void copyDataBase() throws IOException {
            InputStream mInput = mContext.getAssets().open(DB_NAME);
            OutputStream mOutput = new FileOutputStream(DB_FILE);
            byte[] mBuffer = new byte[1024];
            int mLength;
            while ((mLength = mInput.read(mBuffer)) > 0) {
                mOutput.write(mBuffer, 0, mLength);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
        }
    
        // Open the database, so we can query it
        public boolean openDataBase() throws SQLException {
            // Log.v("DB_PATH", DB_FILE.getAbsolutePath());
            mDataBase = SQLiteDatabase.openDatabase(DB_FILE, null, SQLiteDatabase.CREATE_IF_NECESSARY);
            // mDataBase = SQLiteDatabase.openDatabase(DB_FILE, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            return mDataBase != null;
        }
    
        @Override
        public synchronized void close() {
            if(mDataBase != null) {
                mDataBase.close();
            }
            super.close();
        }
    
    }
    

    Write a DataAdapter class like:

    import java.io.IOException;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;
    
    public class TestAdapter {
    
        protected static final String TAG = "DataAdapter";
    
        private final Context mContext;
        private SQLiteDatabase mDb;
        private DataBaseHelper mDbHelper;
    
        public TestAdapter(Context context) {
            this.mContext = context;
            mDbHelper = new DataBaseHelper(mContext);
        }
    
        public TestAdapter createDatabase() throws SQLException {
            try {
                mDbHelper.createDataBase();
            } catch (IOException mIOException) {
                Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
                throw new Error("UnableToCreateDatabase");
            }
            return this;
        }
    
        public TestAdapter open() throws SQLException {
            try {
                mDbHelper.openDataBase();
                mDbHelper.close();
                mDb = mDbHelper.getReadableDatabase();
            } catch (SQLException mSQLException) {
                Log.e(TAG, "open >>"+ mSQLException.toString());
                throw mSQLException;
            }
            return this;
        }
    
        public void close() {
            mDbHelper.close();
        }
    
         public Cursor getTestData() {
             try {
                 String sql ="SELECT * FROM myTable";
                 Cursor mCur = mDb.rawQuery(sql, null);
                 if (mCur != null) {
                    mCur.moveToNext();
                 }
                 return mCur;
             } catch (SQLException mSQLException) {
                 Log.e(TAG, "getTestData >>"+ mSQLException.toString());
                 throw mSQLException;
             }
         }
    }
    

    Now you can use it like:

    TestAdapter mDbHelper = new TestAdapter(urContext);
    mDbHelper.createDatabase();
    mDbHelper.open();
    
    Cursor testdata = mDbHelper.getTestData();
    
    mDbHelper.close();
    

    EDIT: Thanks to JDx

    For Android 4.1 (Jelly Bean), change:

    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    

    to:

    DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    

    in the DataHelper class, this code will work on Jelly Bean 4.2 multi-users.

    EDIT: Instead of using hardcoded path, we can use

    DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath();
    

    which will give us the full path to the database file and works on all Android versions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already have an application that uses the SQLite database and a listadapter. I
I am developing an android application in which I have created a database named
I guess many people already read this article: Using your own SQLite database in
i am creating an app that needs a database. i created it using sqlite
I have created a .net application that is currently using a encrypted text file
I have already created a table to the database. Table is something like following
I have created a database using the SQLite administrator. It contains 3 tables and
I already have a sqlite3 db file that I created in Windows, is there
I have an application published in the android market, and now I want to
Is it possible to rename a database already created in android? On my apps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.