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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:16:27+00:00 2026-05-25T17:16:27+00:00

I’m working on an application which uses SQLite Database on Android.I have a custom

  • 0

I’m working on an application which uses SQLite Database on Android.I have a custom DatabaseHelper class,which creates two different sqlite databases and copy them from the assets folder.The problem that I have is that I can’t set which database to use in different situations.Here is how my DatabaseHelper class looks like:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper{

    private static SQLiteDatabase sqliteDb;
    private static DataBaseHelper instance;
    private static final int DATABASE_VERSION = 1;
    // the default database path is :
    // /data/data/pkgNameOfYourApplication/databases/
    private static String DB_PATH_PREFIX = "/data/data/";
    private static String DB_PATH_SUFFIX = "/databases/";
    private static final String TAG = "DataBaseHelper";
    private Context context;

    /***
     * Contructor
     * 
     * @param context
     *            : app context
     * @param name
     *            : database name
     * @param factory
     *            : cursor Factory
     * @param version
     *            : DB version
     */
    public DataBaseHelper(Context context, String name,
                    CursorFactory factory, int version) {
            super(context, name, factory, version);
            this.context = context;
            Log.i(TAG, "Create or Open database : " + name);
    }

    /***
     * Initialize method
     * 
     * @param context
     *            : application context
     * @param databaseName
     *            : database name
     */
    public static void initialize(Context context, String databaseName) {
            if (instance == null) {
                    /**
                     * Try to check if there is an Original copy of DB in asset
                     * Directory
                     */
                    if (!checkDatabase(context, databaseName)) {
                            // if not exists, I try to copy from asset dir
                            try {
                                copyDataBase(context, databaseName);
                            } catch (IOException e) {
                                    Log.e(TAG,"Database "+ databaseName+" does not exists and there is no Original Version in Asset dir");
                            }
                    }

                    Log.i(TAG, "Try to create instance of database (" + databaseName
                                    + ")");
                    instance = new DataBaseHelper(context, databaseName,
                                    null, DATABASE_VERSION);
                    sqliteDb = instance.getWritableDatabase();
                    Log.i(TAG, "instance of database (" + databaseName + ") created !");
            }
    }

    /***
     * Static method for getting singleton instance
     * 
     * @param context
     *            : application context
     * @param databaseName
     *            : database name
     * @return : singleton instance
     */
    public static final DataBaseHelper getInstance(
                    Context context, String databaseName) {
            initialize(context, databaseName);
            return instance;
    }

    /***
     * Method to get database instance
     * 
     * @return database instance
     */
    public SQLiteDatabase getDatabase() {
            return sqliteDb;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            Log.d(TAG, "onCreate : nothing to do");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.d(TAG, "onUpgrade : nothing to do");

    }

    /***
     * Method for Copy the database from asset directory to application's data
     * directory
     * 
     * @param databaseName
     *            : database name
     * @throws IOException
     *             : exception if file does not exists
     */
    public void copyDataBase(String databaseName) throws IOException {
            copyDataBase(context, databaseName);
    }

    /***
     * Static method for copy the database from asset directory to application's
     * data directory
     * 
     * @param aContext
     *            : application context
     * @param databaseName
     *            : database name
     * @throws IOException
     *             : exception if file does not exists
     */
    private static void copyDataBase(Context aContext, String databaseName)
                    throws IOException {

            // Open your local db as the input stream
            InputStream myInput = aContext.getAssets().open(databaseName);

            // Path to the just created empty db
            String outFileName = getDatabasePath(aContext, databaseName);

            Log.i(TAG, "Check if create dir : " + DB_PATH_PREFIX
                            + aContext.getPackageName() + DB_PATH_SUFFIX);

            // if the path doesn't exist first, create it
            File f = new File(DB_PATH_PREFIX + aContext.getPackageName()
                            + DB_PATH_SUFFIX);
            if (!f.exists())
                    f.mkdir();

            Log.i(TAG, "Trying to copy local DB to : " + outFileName);

            // 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 = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

            Log.i(TAG, "DB (" + databaseName + ") copied!");
    }

    /***
     * Method to check if database exists in application's data directory
     * 
     * @param databaseName
     *            : database name
     * @return : boolean (true if exists)
     */
    public boolean checkDatabase(String databaseName) {
            return checkDatabase(context, databaseName);
    }

    /***
     * Static Method to check if database exists in application's data directory
     * 
     * @param aContext
     *            : application context
     * @param databaseName
     *            : database name
     * @return : boolean (true if exists)
     */
    private static boolean checkDatabase(Context aContext, String databaseName) {
            SQLiteDatabase checkDB = null;

            try {
                    String myPath = getDatabasePath(aContext, databaseName);

                    Log.i(TAG, "Trying to conntect to : " + myPath);
                    checkDB = SQLiteDatabase.openDatabase(myPath, null,
                                    SQLiteDatabase.OPEN_READONLY);
                    Log.i(TAG, "Database " + databaseName + " found!");
                    checkDB.close();
            } catch (SQLiteException e) {
                    Log.i(TAG, "Database " + databaseName + " does not exists!");

            }

            return checkDB != null ? true : false;
    }

    /***
     * Method that returns database path in the application's data directory
     * 
     * @param databaseName
     *            : database name
     * @return : complete path
     */
    @SuppressWarnings("unused")
    private String getDatabasePath(final String databaseName) {
            return getDatabasePath(context, databaseName);
    }

    /***
     * Static Method that returns database path in the application's data
     * directory
     * 
     * @param aContext
     *            : application context
     * @param databaseName
     *            : database name
     * @return : complete path
     */
    private static String getDatabasePath(Context aContext, String databaseName) {
            return DB_PATH_PREFIX + aContext.getPackageName() + DB_PATH_SUFFIX
                            + databaseName;
    }

    public boolean executeQuery(String tableName,ContentValues values){
        return execQuery(tableName,values);
    }

    private static boolean execQuery(String tableName,ContentValues values){
        sqliteDb = instance.getWritableDatabase();
        sqliteDb.insert(tableName, null, values);
        return true;
    }

    public boolean updateSQL(String tableName,String key,String value){
        return updateData(tableName,key,value); 
    }

    private static boolean updateData(String tableName,String key,String value){
        sqliteDb = instance.getWritableDatabase();
        String where = "";
        ContentValues values = new ContentValues();
        values.put(key, value);
        values.put(key, value);
        sqliteDb.update(tableName, values, where, new String[] {"3"});
        return true;
    }

    public boolean deleteSQL(String tableName){
        return deleteData(tableName);
    }

    private static boolean deleteData(String tableName){
        sqliteDb = instance.getWritableDatabase();
        String where = "";
        sqliteDb.delete(tableName, where, new String[] {"5"});
        return true;
    }

    public Cursor executeSQLQuery(String query){
        return sqliteDb.rawQuery(query,null);
    }


    /**
     * Make queries 
     * 
     */
}

I have two database : the first one is system.sqlite and the second one user.sqlite.
I’m initializing the first one when my app starts :

dbHelper = new DataBaseHelper(this, "system.sqlite", null, 1);
DataBaseHelper.initialize(this, "system.sqlite");
dbHelper.checkDatabase("system.sqlite");

I’m initializing the user’s database the same way, when user first login in my application.
After the login I have a few non-activity classes,where I need to insert some data in both of db’s :

ContentValues values = new ContentValues();
values.put("objectId", 75);
values.put("objectOid", "boom");
values.put("serverName", "shit");
values.put("locale", "en_US");
values.put("deviceId", 45);
dbHelper.executeQuery("users",values);

But the problem is that even when open my system.sqlite in the first class than close it..and try to open the user.sqlite from another class it’s still trying to write to system db. Example :

dbHelper = new DataBaseHelper(context, "system.sqlite", null, 1);
dbHelper.initialize(context, "system.sqlite");
dbHelper.getWritableDatabase();
//do some work
dbHelper.close();

It’s not actually working.
Any suggestions how can I get the things to work?

  • 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-25T17:16:28+00:00Added an answer on May 25, 2026 at 5:16 pm

    Why don’t you have separate DataBaseHelper classes? Such as UserDataBaseHelper and SystemDataBaseHelper. I know this sounds simplistic, but if you had a larger project, with two databases, would you lump all the db access into one class?

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

Sidebar

Related Questions

No related questions found

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.