android – Specify max sqlite db size for my app. How to handle db full exception-ThrowExceptions

Exception or error:

I was asked in an interview how can we set max size for sqlite db for our android app. And how can we handle when max size is reached?

EDIT: Marina Lynn’s answer below seems acceptable to this question. But Are there some sort of listeners which are triggered when the db size reaches near the limit we have specified? (Or something like this)

How to solve:

First, to set the maximum database size use .setMaximumSize() as mentioned in the previous answer.

Second, Android throws SQLiteFullException if the database file grows larger than maximum number of pages * page size. SQLiteFullException is a RuntimeException. If you want to handle it and send a relevant response you will do something like:

try {
     getContentResolver().update(myEntity.CONTENT_URI, values, where, null);
} catch (SQLiteFullException e) {
    Log.w(TAG, "Exception when updating ...", e);
}

###

Although there is no limit on the size of the database except for available storage, you can set the maximum size the database will grow to by SQLiteDatabase.setMaximumSize(). If you reach the limit you have to shrink your database. Or you can move your database from internal memory to external storage possibly with more free space available and increase database maximum size. But heavy databases lead to bad user experience so second option consider theoretic. Remember we are in mobile environment not in a dedicated database server.

###

Pls take it to consideration SQLiteFullException will be throw not only when you database reach it’s own limit but also in situation there is no free space in file storage. If you want to handle SQLiteFull exception you also need to check free space on the device. If it out of space on the device you need to tell to user to clear device from unused data.

Leave a Reply

Your email address will not be published. Required fields are marked *