21

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)

  • What to do when max size is reached is not a useful question, because then it's too late. You should try to avoid ever reaching the limit by deleting old data; see also http://www.sqlite.org/pragma.html#pragma_auto_vacuum . – CL. Sep 02 '12 at 12:40
  • I disagree with @CL. Using SQLite as a persistent cache is a legit use case of the maximum size, thus the question is useful. While inserting a new record, it throws SQLiteFullException, so you can purge the oldest record and retry (in a while loop if it's necessary to remove several records to make space for the new one). Implementation note: if the database is empty and you still cannot insert, abort since the single record is bigger than the maximum size. – Guillaume Perrot Sep 05 '12 at 12:37
  • 1
    Marinna's answer is def suffice. You don't need a "listener" here because the exception you get will be synchronous to the operation that you are trying to do. If you just wrap the update/insert functions and check for the exception. If you care about the "limit" that you are given, you can always set the limit to be somewhat less than the actual limit to achieve this. Keep in mind the goal of this is to maintain database size, not to limit the user or yourself in other ways. Besides that, a listener won't be helpful since you could be in anywhere in the application anyways. – Edison Sep 07 '12 at 06:31

3 Answers3

17

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);
}
Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
Marina Lynn
  • 471
  • 3
  • 7
  • 1
    My application doesnt catch that exception. Even if i'm using sqlDB.insertOrThrow() inside of my provider..... Help me :/. Please, post an exemple of contentProvider that will works with that }catch (SQLiteFullException e) { – Renan Franca Dec 04 '12 at 00:40
9

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.

biegleux
  • 13,179
  • 11
  • 45
  • 52
  • thanx for the answer, but how can I know that limit has been reached –  Jul 24 '12 at 06:59
  • @djaqeel I've never come across any built in listeners. But, you could just check the size of the database file in the filesystem to get a rough idea. Something like: new File(Environment.getDataDirectory(), "data/" + context.getPackageName() + "/databases/" + databaseName).length, or pass that path to StatFs as biegleux suggests. – newbyca Sep 07 '12 at 03:20
0

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.