Filip Luchianenco
Filip Luchianenco

Reputation: 7012

Clear all database SQL tables in Android

Here is my DatabaseHelper which saves everything. How do i erase/clean/clear all the tables?

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = "DatabaseHelper";

private static final String DATABASE_NAME = "user.db";
private static final int DATABASE_VERSION = 1;

private RuntimeExceptionDao<CurrentUserModel, Long> currentUserModelDao;
private RuntimeExceptionDao<Contact, String> contactDao;
private RuntimeExceptionDao<ChatModel, Long> chatDao;
private RuntimeExceptionDao<ChatMessage, Long> chatMessageDao;
private RuntimeExceptionDao<ChatContact, Long> chatContactDao;
private RuntimeExceptionDao<BroadcastMessage, Long> broadcastMessageDao;

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

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connection) {
    try {
        TableUtils.createTable(connection, CurrentUserModel.class);
        TableUtils.createTable(connection, Contact.class);
        TableUtils.createTable(connection, ChatModel.class);
        TableUtils.createTable(connection, ChatMessage.class);
        TableUtils.createTable(connection, ChatContact.class);
        TableUtils.createTable(connection, BroadcastMessage.class);
    } catch (SQLException e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connection, int arg2, int arg3) {
    try {
        TableUtils.dropTable(connection, CurrentUserModel.class, true);
        TableUtils.dropTable(connection, Contact.class, true);
        TableUtils.dropTable(connection, ChatModel.class, true);
        TableUtils.dropTable(connection, ChatMessage.class, true);
        TableUtils.dropTable(connection, ChatContact.class, true);
        TableUtils.dropTable(connection, BroadcastMessage.class, true);
        onCreate(db, connection);
    } catch (SQLException e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
    }
}

public RuntimeExceptionDao<CurrentUserModel, Long> getCurrentUserModelDao() {
    if (currentUserModelDao == null) {
        try {
            currentUserModelDao = RuntimeExceptionDao.createDao(getConnectionSource(), CurrentUserModel.class);
            currentUserModelDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return currentUserModelDao;
}

public RuntimeExceptionDao<Contact, String> getContactDao() {
    if (contactDao == null) {
        try {
            contactDao = RuntimeExceptionDao.createDao(getConnectionSource(), Contact.class);
            contactDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return contactDao;
}

public RuntimeExceptionDao<ChatModel, Long> getChatDao() {
    if (chatDao == null) {
        try {
            chatDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatModel.class);
            chatDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return chatDao;
}

public RuntimeExceptionDao<ChatMessage, Long> getChatMessageDao() {
    if (chatMessageDao == null) {
        try {
            chatMessageDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatMessage.class);
            chatMessageDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return chatMessageDao;
}

public RuntimeExceptionDao<ChatContact, Long> getChatContactDao() {
    if (chatContactDao == null) {
        try {
            chatContactDao = RuntimeExceptionDao.createDao(getConnectionSource(), ChatContact.class);
            chatContactDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return chatContactDao;
}

public RuntimeExceptionDao<BroadcastMessage, Long> getBroadcastMessageDao() {
    if (broadcastMessageDao == null) {
        try {
            broadcastMessageDao = RuntimeExceptionDao.createDao(getConnectionSource(), BroadcastMessage.class);
            broadcastMessageDao.setObjectCache(true);
        } catch (SQLException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }
    return broadcastMessageDao;
}

@Override
public void close() {
    super.close();
    if (currentUserModelDao != null) {
        currentUserModelDao.clearObjectCache();
        currentUserModelDao = null;
    }
    if (contactDao != null) {
        contactDao.clearObjectCache();
        contactDao = null;
    }
    if (chatDao != null) {
        chatDao.clearObjectCache();
        chatDao = null;
    }
    if (chatMessageDao != null) {
        chatMessageDao.clearObjectCache();
        chatMessageDao = null;
    }
    if (chatContactDao != null) {
        chatContactDao.clearObjectCache();
        chatContactDao = null;
    }
    if (broadcastMessageDao != null) {
        broadcastMessageDao.clearObjectCache();
        broadcastMessageDao = null;
    }
}

}

I am trying to make some kind of logout, which would clear all the data. Here is what I have tried:

DatabaseHelper dh = new DatabaseHelper(getActivity().getBaseContext());
            dh.close();
            clearApplicationData();

This works only if after these 2 rows were done, I kill the app from recent and start it again, else the app tries to register again, and crashes when tries to use the database. how do I correctly reset all application data as it was when it was installed?

Upvotes: 1

Views: 2700

Answers (1)

Martin Nuc
Martin Nuc

Reputation: 5764

The easiest way to erase all tables is to actually delete whole database.

DatabaseHelper databaseHelper = getHelper();
databaseHelper.close();
while (databaseHelper.isOpen() == true)  {   // maybe you dont want to use while
    Thread.sleep(500);
}
this.deleteDatabase("database.db"); // specified in DatabaseHelper class in the DATABASE_NAME field

After doing this you have to create a new database helper (it recreates database) otherwise you would get exception saying something like "unable to open already closed object":

OpenHelperManager.releaseHelper();
OpenHelperManager.setHelper(new DatabaseHelper(this));

Upvotes: 4

Related Questions