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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:14:47+00:00 2026-05-31T13:14:47+00:00

In my app, after user logs in, database is created. When user logs out,

  • 0

In my app, after user logs in, database is created. When user logs out, I have to delete the database from the internal storage to save space. The problem is, after deleting the database and a user logs back in, database cannot be created anymore. I tried using .close() but it only makes the problem worse.
Here is my code.
DatabaseHelper

    public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static final String DATABASE_PATH = "/mnt/sdcard/Philpost/databases/";

private static final String DATABASE_NAME = "DeliveriesDB.sqlite";

private static final int DATABASE_VERSION = 1;

// the DAO object we use to access the SimpleData table
private Dao<DeliveriesDB, Integer> DeliveriesDbDao = null;

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

@Override
public void onCreate(SQLiteDatabase database,
        ConnectionSource connectionSource) {
    try {
        TableUtils.createTable(connectionSource, DeliveriesDB.class);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
        throw new RuntimeException(e);
    } catch (java.sql.SQLException e) {
        e.printStackTrace();
    }

}

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
        int oldVersion, int newVersion) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onUpgrade");
        TableUtils.dropTable(connectionSource, DatabaseHelper.class, true);
        onCreate(db, connectionSource);
    } catch (java.sql.SQLException e) {
        // TODO Auto-generated catch block
        Log.e(DatabaseHelper.class.getName(), "Cant drop database", e);
        e.printStackTrace();
    }
}

public Dao<DeliveriesDB, Integer> getDeliveriesDbDao() {
    if (null == DeliveriesDbDao) {
        try {
            DeliveriesDbDao = getDao(DeliveriesDB.class);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    return DeliveriesDbDao;
}

    }

DatabaseManager

    public class DatabaseManager {

static private DatabaseManager instance;

static public void init(Context ctx) {
    if (null == instance) {
        instance = new DatabaseManager(ctx);
    }
}

static public DatabaseManager getInstance() {
    return instance;
}

private DatabaseHelper helper;

public DatabaseManager(Context ctx) {
    helper = new DatabaseHelper(ctx);
}

public DatabaseHelper getHelper(Context ctx) {
    if(helper == null){
        helper = OpenHelperManager.getHelper(ctx, DatabaseHelper.class);
    }
    return helper;
}

public void releaseDb(Context ctx) {
    DatabaseConnection connect;
    try {
        connect = getHelper(ctx).getConnectionSource()
                .getReadWriteConnection();
        getHelper(ctx).getConnectionSource().releaseConnection(connect);
        helper = null;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void closeDb(){
    helper.close();
}

public List<DeliveriesDB> getAllDeliveriesDB(Context ctx) {
    List<DeliveriesDB> deliveriesdb = null;
    try {
        deliveriesdb = getHelper(ctx).getDeliveriesDbDao().queryForAll();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return deliveriesdb;
}

public void addDeliveriesDb(DeliveriesDB l, Context ctx) {
    try {
        getHelper(ctx).getDeliveriesDbDao().create(l);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public DeliveriesDB getDeliveriesDbWithId(int deliveriesDbId, Context ctx) {
    DeliveriesDB deliveriesDb = null;
    try {
        deliveriesDb = getHelper(ctx).getDeliveriesDbDao().queryForId(
                deliveriesDbId);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return deliveriesDb;
}

public void deleteDeliveriesDb(DeliveriesDB deliveriesDb, Context ctx) {
    try {
        getHelper(ctx).getDeliveriesDbDao().delete(deliveriesDb);
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public void refreshDeliveriesDb(DeliveriesDB deliveriesDb, Context ctx) {
    try {
        getHelper(ctx).getDeliveriesDbDao().refresh(deliveriesDb);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void updateDeliveriesDb(DeliveriesDB deliveriesDb, Context ctx) {
    try {
        getHelper(ctx).getDeliveriesDbDao().update(deliveriesDb);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

    }

The class where creation and deletion of database happens

    public class DeliveryListActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DatabaseManager.init(this);

    setContentView(R.layout.deliverylist_layout);

    if (getLastNonConfigurationInstance() != null) {
        deliveryIndex = (Integer) getLastNonConfigurationInstance();
    }
    if (PhilpostApplication.DELIVERIES == null) {
        new RetrieveDeliveriesTask().execute();
    } else {
        updateCachedList(PhilpostApplication.DELIVERIES);
    }
}

private void updateCachedList(List<Delivery> deliveries) {
    File expath = context.getFilesDir();
    String apppath = "/databases/DeliveriesDB.sqlite";
    File path = new File(expath, apppath);

    adapter = new DeliveryListAdapter(this,
            R.layout.deliverylist_row_layout, deliveries);
    setListAdapter(adapter);
    PhilpostApplication.DELIVERIES = deliveries;
    Log.d(TAG, "Updating UI list");
    if (PhilpostApplication.firstDb) {
        if (!path.exists()) {
            createBackupDb();
            Log.d(TAG, "DB first Creation");
        }
    }
}

public void createBackupDb() {

    for (int i = 0; i < PhilpostApplication.DELIVERIES.size(); i++) {
        // create db first

        dId = PhilpostApplication.DELIVERIES.get(i).getId();
        rId = PhilpostApplication.DELIVERIES.get(i).getRecipientId();
        lastn = PhilpostApplication.DELIVERIES.get(i).getLastName();
        firstn = PhilpostApplication.DELIVERIES.get(i).getFirstName();
        addr = PhilpostApplication.DELIVERIES.get(i).getAddress();
        dtype = PhilpostApplication.DELIVERIES.get(i).getType();
        amount = PhilpostApplication.DELIVERIES.get(i).getCash();

        pMan = PhilpostApplication.DELIVERIES.get(i).getPostman();
        stats = PhilpostApplication.DELIVERIES.get(i).getStatus();

        createNewDeliveriesDb(dId, rId, lastn, firstn, addr, dtype, amount,
                pMan, stats);
        keyNum[i] = PhilpostApplication.DELIVERIES.get(i).getId();
    }
    Log.d(TAG, "database created");
    PhilpostApplication.firstDb = false;
}
public void logout() {

    if (PhilpostApplication.listSynced == false) {
        // if( checkIfSyncedList() ){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Sync data first before logging out.")
                .setCancelable(false).setPositiveButton("OK", null);
        final AlertDialog alert = builder.create();
        alert.show();
    } else {

        dialog = ProgressDialog.show(this, "Logging out", "please wait");
        try {
            WebService.logout();
            PhilpostApplication.SESSION_KEY = null; // clear Application
                                                    // Session
            // Key
            AccountStore.clear(this);

            // clear cached list

            PhilpostApplication.DELIVERIES = null;
            MemoryUtils.deleteCache(this);
            PhilpostApplication.incompleteSync = false;
            PhilpostApplication.loggedIn = false;
            PhilpostApplication.firstDb = true;

            DatabaseManager.getInstance().closeDb();
            deleteInternalDb();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (PhilpostApplication.canToggleGPS) {
            turnGpsOff();

        }

        dialog.dismiss();
        exitActivity();
    }
}
    }

Deleting Database

    public void deleteInternalDb() {
    File internalDb = new File(
            Environment.getDataDirectory()
                    + "/data/packagename/databases/DeliveriesDB.sqlite");
    if (internalDb.exists()) {
        internalDb.delete();
        Log.d(TAG, "Internal Db deleted");
    }
}
  • 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-31T13:14:47+00:00Added an answer on May 31, 2026 at 1:14 pm

    Check the example here this will give you an idea how to use existing database.

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

Sidebar

Related Questions

In my web app after a user logs in a new session is created
I have an ActiveAdmin app. When the user logs in, it is taken to
In a Swing app a method should continue only after user enters a correct
My app has a session timeout after 30 minutes. If the user has a
After clicking on a UIView I want to take the user of my app
after launching your app on google app engine. you can use the 'logs' page
Weird problem: After rotating my app to portrait, picking the toolbar item and exposing
I am making a Music player for my web app. After user upload some
When a user logs in to a Django app, how do I change the
I have a rails app that is using Devise, with a User model, no

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.