开发者

How do I find the size of an SQLite database in an iPhone application?

开发者 https://www.devze.com 2022-12-12 11:25 出处:网络
In my iphone application,i have used sqlite3 for storing the data. 开发者_JAVA技巧how to get the size of the database using the iphone functionality?

In my iphone application,i have used sqlite3 for storing the data. 开发者_JAVA技巧how to get the size of the database using the iphone functionality?

if anybody has any code or any useful link or any other resolution,which would be appreciated.

Thanks,

Mishal Shah


You can use the following code (drawn from my answer to this question) to determine both your database's size and the free space on the filesystem:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *persistentStorePath = [documentsDirectory stringByAppendingPathComponent:@"database.sqlite"];

NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:persistentStorePath error:&error];
NSLog(@"Persistent store size: %@ bytes", [fileAttributes objectForKey:NSFileSize]);

NSDictionary *fileSystemAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:persistentStorePath error:&error];
NSLog(@"Free space on file system: %@ bytes", [fileSystemAttributes objectForKey:NSFileSystemFreeSize]);

This assumes that your database is called "database.sqlite" and is stored at the root of your application's documents directory.


Try the following, this could help you

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (void) logSqlliteStoreSize
{
    NSString *yourSqlLiteFileName = @"YOURDATABASE_NAME.sqlite";
    NSString *yourSqlLitePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: yourSqlLiteFileName];
    NSError *error;
    NSDictionary *storeAttributesOfItemAtPath = [[NSFileManager defaultManager] attributesOfItemAtPath:yourSqlLitePath error:&error];
    // here you should test for errors, this is a simplified version

    // the next log will show much information about your sqlite file
    // including the filesize
    NSLog(@"storeAttributesOfItemAtPath=%@", storeAttributesOfItemAtPath);

}


You can find the database file, of the application in the iPhone simulator, in

~/Library/Application Support/iPhone Simulator/User/Applications/*a long number*/Documents/

If you have a jail broken phone you can SSH into it and find the applications folder where it will also be in a /Applications/*<a long number>*/Documents/ folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消