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

  • SEARCH
  • Home
  • 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 9010969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:36:14+00:00 2026-06-16T02:36:14+00:00

When I try to open a database in the assets folder with the method

  • 0

When I try to open a database in the assets folder with the method

  public void loadDatabase(String dbName){
    System.out.println("dbName");
    DataBaseAdapterNew mDbHelper = new DataBaseAdapterNew(
                    this, dbName);
            mDbHelper.createDatabase();
            mDbHelper.open();

    }

I get a fatal Exception ErrorCopyingDataBase

Error Stack

12-13 18:21:29.179: E/AndroidRuntime(10230): FATAL EXCEPTION: main
12-13 18:21:29.179: E/AndroidRuntime(10230): java.lang.Error: ErrorCopyingDataBase
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.db.DataBaseAdapterNew.createDatabase(DataBaseAdapterNew.java:42)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.MyActivity.loadDatabase(MyActivity.java:133)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.my.app.MyActivity$InvocazioneAsincrona$1.run(MyActivity.java:173)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Handler.handleCallback(Handler.java:587)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.os.Looper.loop(Looper.java:130)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invoke(Method.java:507)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-13 18:21:29.179: E/AndroidRuntime(10230):    at dalvik.system.NativeStart.main(Native Method)

My DatabaseAdapterNew class is:

public class DataBaseAdapterNew {

    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public DataBaseAdapterNew(Context context, String nameDB) {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext, nameDB);
    }

    public DataBaseAdapterNew createDatabase() throws SQLException {
        try {
            mDbHelper.createDataBase();
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public DataBaseAdapterNew 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 allData() {
        try {
            Cursor c = mDb.rawQuery(
                    "SELECT * FROM myTable order by Quantity desc", null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "allData >>" + mSQLException.toString());
            throw mSQLException;
        }

    }



    }

My DatabaseHelper class is:

public class DataBaseHelper extends SQLiteOpenHelper {
    private static String TAG = "DataBaseHelper"; 
    private static String DB_PATH = "";
    private static String DB_NAME = "";
    private SQLiteDatabase mDataBase;
    private final Context mContext;

    public DataBaseHelper(Context context, String nomeDB) {
        super(context, nomeDB, null, 1);// 1? its Database Version
        DB_NAME = nomeDB;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        this.mContext = context;
    }

    public void createDataBase() throws IOException {
        // If database not exists copy it from the assets

        boolean mDataBaseExist = checkDataBase();
        if (!mDataBaseExist) {
            this.getReadableDatabase();
            this.close();
            try {
                // Copia il database dalla cartella assests
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                throw new Error("ErrorCopyingDataBase");
            }
        }
    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    private void copyDataBase() throws IOException {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    public boolean openDataBase() throws SQLException {
        String mPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null,
                SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase != null;
    }

    @Override
    public synchronized void close() {
        if (mDataBase != null)
            mDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

The passed String dbName shows the right name of the db present in the assets folder in println, and the database is non null and precompiled, so I cannot figure what is the cause of the error.
Any suggestion?

  • 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-16T02:36:15+00:00Added an answer on June 16, 2026 at 2:36 am

    I copied your code into my project, and it works correctly.

    In loadDatabase(),

    System.out.println("dbName");
    

    will always print the word dbName. I’m not sure if that is the name of your database in the assets folder; as I cannot see your call to loadDatabase(dbName), I do not know what value you are using.

    This is how I invoked your code, directly from my application:

        String dbName = "itr";
        System.out.println(dbName);
        DataBaseAdapterNew mDbHelper = new DataBaseAdapterNew(
                        this, dbName);
                mDbHelper.createDatabase();
                mDbHelper.open();
    

    In DatabaseHelper.createDataBase(), you are hiding the error condition. If you want to see what exception is causing the problem, may I suggest

    public void createDataBase() throws IOException {
        // If database not exists copy it from the assets
    
        boolean mDataBaseExist = checkDataBase();
        if (!mDataBaseExist) {
            this.getReadableDatabase();
            this.close();
            try {
                // Copia il database dalla cartella assests
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                Log.e(TAG, mIOException.toString());
                throw mIOException;
            }
        }
    }
    

    That printed the exception

    java.io.FileNotFoundException: itr UnableToCreateDatabase

    I copied over an existing database into /assets/itr and my database was created correctly.

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

Sidebar

Related Questions

I've got this database taken from assets folder, but i can't open it in
Currently, I am copying a database from my assets folder to the to the
I try to copy SQLite database from assets directory to access it later. But
I try to open the database YourGuruDB1 in this directory: D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data
I have an sqlite file in the assets folder. I'm using a database manager
I have a database in SQLite precreated and inserted in assets folder, unfortunately I
I'm outputting values from a database (it isn't really open to public entry, but
For some reason, whenever I try to copy over a database from my assets
I use this tutorial: Tutorial Custom database I copy database from assets folder and
I'm making Android app that has this database in it: public void onCreate(SQLiteDatabase db)

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.