Bedo
Bedo

Reputation: 937

How to get the current sqlite database size or package size in Android?

I can't get the total amount of data used by an Android Application (or package), because the official API support has been deleted:

http://groups.google.com/group/android-developers/browse_thread/thread/df94daae34336aad/f96a8425549c6637 (is this still the current situation?)

Getting installed app size (unsopported hack - do not use it)

So is there any way to get the current sqlite database size? I've found a getMaxSize method or getPageSize but I'm not able to find the total number of pages.

Upvotes: 20

Views: 35673

Answers (4)

gerbit
gerbit

Reputation: 962

You can use this raw query as well, on sqlite 3.16.0+:

SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();

It returns the "size" column with the single row.

Upvotes: 20

Britc
Britc

Reputation: 633

Just to add-on about the size.

When I used the code by DroidBot, it works. But the size may not straightaway increase when data is entered into the database (especially if the data is very small). Database will only increment in 1024 bytes (e.g from 8192 bytes to 9216 bytes).

So, to see the database size increase straightaway, try putting in a large amount of data.

Upvotes: 5

DroidBot
DroidBot

Reputation: 361

Perhaps someone could verify if this is an acceptable approach, but for a while I was using this:

File f = context.getDatabasePath(dbName);
long dbSize = f.length();

Cheers

Upvotes: 36

Mat
Mat

Reputation: 206679

You can query the database with these two:

pragma page_size;
pragma page_count;

(see the sqlite pragma docs).

Multiply the first by the second and you'll get total pages used.

Upvotes: 21

Related Questions