7

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • How are you accessing the database in the simulator? – WrightsCS Jul 23 '11 at 15:26
  • The path above leads me to the sqlite file. – Nitish Jul 23 '11 at 15:29
  • 1
    That path above leads you to the application root. In that directory, there should be a `Documents` directory along with your application bundle. – WrightsCS Jul 23 '11 at 15:31
  • I have updated the path. Earlier I just forgot to write DB file at the end. But this is true in case of simulator only I guess. If we are working on device, is it really possible to access the DB file? – Nitish Jul 23 '11 at 15:51
  • your path suggests that your db is at your application root.. which is, wrong.. how did you ever get you db there? you should place it inside the documents directory... and that is where you should access it from... – Swapnil Luktuke Jul 25 '11 at 10:25

8 Answers8

21

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.

Shimon
  • 1,434
  • 1
  • 10
  • 9
  • The document folder is empty in my case too, I think Xcode no longer copies the sqlite file or it has moved on IOS to another location. – Wayne Jun 26 '17 at 17:16
11

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.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
  • And where do you implement this method? – Nitish Jul 23 '11 at 15:58
  • I use an EntityManager class which is really useful! Here's the [header](http://dl.dropbox.com/u/3722472/Coding/Objective-C/EntityManager.h.txt) and the [implementation](http://dl.dropbox.com/u/3722472/Coding/Objective-C/EntityManager.m.txt) of it. – Christian Schnorr Jul 23 '11 at 16:09
  • Thanks Jenox. Unfortunately I dont have device with me. I'll definitely try it tomorrow. Thanks a lot. If things work fine, I'll accept your answer. – Nitish Jul 23 '11 at 16:14
  • Hi, I am facing different kind of problem to get sqlite database. My developer profile certificate expired and my app is not working but I can't reinstall it. Because I want to get that data first after that I want to move. How I can get that data please guide me. – Code Hunter Sep 04 '13 at 12:02
5

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];
Kaiser
  • 688
  • 4
  • 12
  • Does this path gives the database on device? Because I want to open it in sqlite browser. I am wondering how can I do that? – Nitish Jul 27 '11 at 06:58
  • Ahh are you trying to open the database from the iphone with an application that is not on the iPhone? – Kaiser Jul 28 '11 at 14:53
  • The application is in IPhone. Lets say we are debugging the IPhoneapplication. Then can we open that application's DB in sqlit browser? – Nitish Jul 29 '11 at 04:10
4

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];
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • Will this return the directory of my sqlite file when I am running my application on device? – Nitish Jul 23 '11 at 15:28
  • this will return the Documents directory. You will need to figure out how to plug in the database name with that string. – WrightsCS Jul 23 '11 at 15:30
  • What you usually do to get that? I am not getting your point. – Nitish Jul 23 '11 at 15:33
  • `[NSString stringWithFormat:@"%@/%@",[self applicationDocumentsDirectory], @"YourDatabase.db"];` – WrightsCS Jul 23 '11 at 15:35
  • Firstly, does this give you the the correct result? Secondly, I need to implement this method in my database class where I have made DB connection or somewhere else? – Nitish Jul 23 '11 at 15:40
  • Your database should never be at the root of your Applications bundle; I am not even sure how it got there but you should move it (fix your code) to the Applications Documents directory as I have demonstrated. – WrightsCS Jul 23 '11 at 17:16
4

@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.

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
Surjit Joshi
  • 3,287
  • 2
  • 18
  • 20
3

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
2

Checkout your Database file path. this may only reason.

Rakesh Bhatt
  • 4,606
  • 3
  • 25
  • 38
0

do

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

this should work for both device and simulator.

Nitish
  • 13,845
  • 28
  • 135
  • 263
Kumaran
  • 687
  • 6
  • 14