Documents folder is empty in simulator. Where is my db.sqlite?

1k Views Asked by At

I read that I should find my sqlite db in this path:

~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents

But the folder is empty. When I run my app in the simulator I am able to query the db. The db is originally located in the resources folder of my app.

I am working with xcode 3.2.6 and OS X 10.6.8

IS there somewhere else where I could look for it?

Many thanks. Lapo

-(void)insertError:(Frage *) f 
           answer2:(NSString *) answer2 
           answer3:(NSString *) answer3 
 {

    int resconn = sqlite3_open([pathDB UTF8String], &database);

    if (resconn == SQLITE_OK) {

        NSString * sql = @"Insert into errors (id, answer2,answer3) values (?,?,?)";
        const char * sqlStatement = [sql UTF8String];

        sqlite3_stmt * compiledStatement;

        if (sqlite3_prepare_v2(database, sqlStatement, -1 , &compiledStatement, NULL) == SQLITE_OK) {
            sqlite3_bind_text(compiledStatement, 1, [f.id UTF8String], -1 , SQLITE_TRANSIENT);
            sqlite3_bind_int(compiledStatement, 2, [answer2 intValue]);
            sqlite3_bind_int(compiledStatement, 3, [answer3 intValue]);
        } 
        if (!sqlite3_step(compiledStatement) == SQLITE_DONE) {
            //
        }   
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);
}
1

There are 1 best solutions below

0
On

In order to access your resources database in the Documents folder of application, you first need to copy it there when the application launches.

If the database file is in the resources folder in your xcode project, it is not copied automatically into the documents directory of the app.

To do so you can use:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSError *error;

NSString *databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YOUR_DB_IN_RESOURCES"];

[[NSFileManager defaultManager] copyItemAtPath:databasePath 
                                                toPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,@"YOUR_DB"]
                                                 error:&error];

You will then be able to access the database that's now in the documents folder using your existing code ([pathDB UTF8String] should point to the documents directory and not the resources).

As you mentioned, you can still use the DB in the resources, but it will be wiped every time you relaunch your app. The trick is to copy the db into your documents folder where it will remain until the app is manually deleted from device (the documents folder is also kept during app updates, so your db remains when users update the app).

Hope this helps :)