Reputation: 10346
Is there a way to get the amount of free space available to an application's SQLite DB? I see that SQLiteDatabase.getMaximumSize() returns the maximum size of a DB, but is there a way to get the current amount of bytes taken up by the database?
Upvotes: 1
Views: 1641
Reputation: 542
To answer your question, yes there is a way to check how much space the DB is currently using. As Joe noted, you can get the file path and check the size of the file.
I used this method before to prevent a database from increasing beyond a specific size. After writing data to the database, I would call a function to check its size and delete rows if necessary.
public synchronized void checkDbSize()
{
File f = m_context.getDatabasePath(DATABASE_NAME);
long dbSize = f.length();
if(dbSize > DB_SIZE_LIMIT)
{
//Delete X rows from DB
}
}
Upvotes: 2
Reputation: 180020
There is no meaningful default maximum size; you will run into the device's storage size limit first.
You could use a tool like sqlite-analyzer to estimate how much space your data is likely to take up when it grows, but there is no easy way to find out at runtime how near the limit you are.
If you fear that your database becomes too big, you have to regularly clean out old data. You cannot use VACUUM (which creates a temporary copy of the DB file), but you might want to enable PRAGMA auto_vacuum, which at least ensures that the unused pages inside your DB file do not grow out of bounds.
Upvotes: 1