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 9194003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:17:59+00:00 2026-06-17T21:17:59+00:00

Possible Duplicate: How to use an existing database with an Android application I have

  • 0

Possible Duplicate:
How to use an existing database with an Android application

I have a sqlite database file, and i want to use it in my app.

How do i do it ?

How do i push my db file to the emulator, and then use it in my code ?

Any solution is welcome.
thx in advance, Tom.

  • 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-06-17T21:18:01+00:00Added an answer on June 17, 2026 at 9:18 pm
    public class AssetDatabaseHelper extends SQLiteOpenHelper {
    
        private String dbName;
        private String db_path;
        private Context context;
    
        /**
         * A helper class to import db files.
         * 
         * @param base
         *            /app context
         * @param dbName
         *            The name of the db in asset folder .
         */
        public AssetDatabaseHelper(Context context, String dbName) {
            super(context, dbName, null, 1);
            this.dbName = dbName;
            this.context = context;
            db_path = "/data/data/" + context.getPackageName() + "/databases/";
        }
    
        /**
         * Check if the database already exist to avoid re-copying the file each
         * time you open the application.
         * 
         * @return true if it exists, false if it doesn't
         */
        public boolean checkExist() {
    
            SQLiteDatabase checkDB = null;
    
            try {
                String myPath = db_path + dbName;
                checkDB = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
    
            } catch (SQLiteException e) {
                e.printStackTrace();
                // database does't exist yet.
    
            } catch (Exception ep) {
                ep.printStackTrace();
            }
    
            if (checkDB != null) {
    
                checkDB.close();
    
            }
    
            return checkDB != null ? true : false;
        }
    
        /**
         * Creates a empty database on the system and rewrites it with your own
         * database.
         * */
        public void importIfNotExist() throws IOException {
    
            boolean dbExist = checkExist();
    
            if (dbExist) {
                // do nothing - database already exist
            } else {
    
                // By calling this method and empty database will be created into
                // the default system path
                // of your application so we are gonna be able to overwrite that
                // database with our database.
                this.getReadableDatabase();
    
                try {
    
                    copyDatabase();
    
                } catch (IOException e) {
    
                    throw new Error("Error copying database");
    
                }
            }
    
        }
    
        private void copyDatabase() throws IOException {
            InputStream is = context.getAssets().open(dbName);
    
            OutputStream os = new FileOutputStream(db_path + dbName);
    
            byte[] buffer = new byte[4096];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
            os.flush();
            os.close();
            is.close();
            this.close();
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    
    }
    

    Based off of this tutorial. You put your sqlite database file in your asset folder and it will copy when you run the code. My version allows multiply databases files since it chooses the path.
    To use it do :

    AssetDatabaseHelper dbHelper = new AssetDatabaseHelper(
                    getBaseContext(), SomeDataBase.SOME_DATABASE_NAME);
            try {
                dbHelper.importIfNotExist();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to use existing database with Android App A buddy and I
Possible Duplicate: How to use an existing database with an Android application how to
Possible Duplicate: How to use my own sqlite database? Full Android Database Helper class
Possible Duplicate: How to use include within a function? i have two file inc.php
Possible Duplicate: PHP - override existing function I want to use mocking to unit
Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicate: insert contacts into database but does not want to duplicate already existing
Possible Duplicate: Is it possible to program iPhone in C++ I have an existing
Possible Duplicate: How to dynamically expand a Memory Mapped File Hi, I have a
Possible Duplicate: Use grep to report back only line numbers I only want to

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.