Nitish
Nitish

Reputation: 14113

How to get Sqlite database file when application is running on device?

I am facing problem in accessing database file when I am running it on device. How can I get that file? There is no problem in accessing file when I am running application on simulator. Like when I am running application on simulator DB file is in :

/Users/Nitish/Library/Application Support/iPhone Simulator/4.3/Applications/1B787527-0608-4BC1-8022-DFDB3CC35F66/mysqlite.sqlite

Where will I find the DB file when application is running on device?

Upvotes: 7

Views: 15845

Answers (8)

Surjit Joshi
Surjit Joshi

Reputation: 3515

@nitish :

I have a code that will access the sqlite database file when application is running on device.

- (void) initDatabase
{

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                        NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory
                            stringByAppendingPathComponent:@"contacts.db"];

    success = [fileManager fileExistsAtPath:writableDBPath];

    dbpath = [writableDBPath UTF8String];

    NSLog(@"path : %@", writableDBPath);

    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate
    //  location.

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                           stringByAppendingPathComponent:@"contacts.db"];

    success = [fileManager fileExistsAtPath:defaultDBPath];
    //if(success) return;


    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success)
    {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.",[error localizedDescription]);
    }

    dbpath = [writableDBPath UTF8String];
    NSLog(@"path : %@", writableDBPath);
}

"NSLog()" will give you path for sqlite database file. It works for both simulator & device. Hope it will work for you.

Upvotes: 4

Shimon
Shimon

Reputation: 1464

Reading all comments and answer, I think nobody actually answered simple question of the author:

How to access sqlite database file while runs on the actual device (it's not an issue in iPhone simulator as author indicated)

Here is what I do:

  1. (once!) Make sure all hidden files are visible on your Mac: In terminal window run: defaults write com.apple.Finder AppleShowAllFiles YES

  2. While in Xcode, open Organizer -> Devices, find your iPhone and from their find your application in Applications folder.

  3. At the bottom click Download and download the application to your desktop (anywhere)

  4. Open Finder, navigate to the downloaded file, right click on it and select Show Package Contents. The view will change to standard finder view with open files in that app.

  5. Go to the Documents folder, find your *.sqlite file, right click on it and choose Open With -> Other

  6. Select SQLite Database Browser (http://sqlitebrowser.sourceforge.net) and enjoy view of your database through database browser.

It takes less than a minute to run the whole process, once you do it once.

Upvotes: 21

Kumaran
Kumaran

Reputation: 687

do

NSString *dbFilePath=[NSHomeDirectory stringByAppendingPathComponent:@"mysqlite.sqlite"];

this should work for both device and simulator.

Upvotes: 0

user610650
user610650

Reputation:

As I don't completely understand what you are trying to do there is a big chance this will not be very helpful; if this isn't helpful let me know and I'll delete it.

If you need to know where a db is created, perhaps you can created one while running the app in your production environment (ie on some iphone) and then retrieve the path using the database_list pragma:

[someone@somewhere ~]$ sqlite3 foobar.db
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> pragma database_list;
0|main|/home/someone/foobar.db

Upvotes: 3

Kaiser
Kaiser

Reputation: 688

As others have said your database should not be within your application's root folder. If you are loading a static database from your bundle (i.e a database preloaded by adding it in xCode) you have to access it by the bundle.

NSString *path = [[NSBundle mainBundle] pathForResource:@"mysqlite" ofType:@"sqlite"];

If you are accessing the file from the documents directory (i.e it has been modified or saved).

NSString *fileName = @"mysqlite.sqlite";
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *path = [directoryPath stringByAppendingPathComponent:fileName];

Upvotes: 5

Christian Schnorr
Christian Schnorr

Reputation: 10776

I use this method for getting the URL to a database:

+ (NSURL *)storeURL
{
    if ([self isExecutingUnitTests])
    {
        NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
        return [NSURL fileURLWithPath:[directory stringByAppendingPathComponent:@"UnitTests/test-database.sqlite"]];
    }
    else
    {
        return [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.sqlite"]];
    }
}

It works fine on the simulator, on the device and even when executing unit tests! UnitTests and Documents should both exist on the level of the .xcodeproj.

Upvotes: 11

WrightsCS
WrightsCS

Reputation: 50697

Most likely your database is in the apps Documents directory. So you would need to call that directory by using:

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

Upvotes: 4

Rakesh Bhatt
Rakesh Bhatt

Reputation: 4606

Checkout your Database file path. this may only reason.

Upvotes: 2

Related Questions