2

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);
}
  • Are you copying the db file from your Resources folder to the Documents folder upon launch of your app ? – Mutix May 23 '12 at 08:09
  • No @Mutix I didn't, as I thought it wouldn'be necessary. Anyway after your suggestion I did copy it. Anyway, that db remains untouched and I cannot retrieve records I have inserted. Thanks – user1412042 May 23 '12 at 10:18
  • Once you have copied it in the Documents folder, you'll need to use the db located in that documents folder to do your sql requests. Can you post some code of your db copying and usage please, that might help us find your problem – Mutix May 23 '12 at 11:26
  • I added some code I used to insert a record. I didn't copy the db programmatically but manually. Should I copy my db into documents folder programmatically? THis sounds strange as I would expect this to happen at install time in Simulator. Thanks for help – user1412042 May 23 '12 at 13:17
  • no that will not happen automatically ... u need to copy it there . – saadnib May 23 '12 at 13:30
  • Thanks @saadnib. Anyway, having copied manually. I made some inserts using the code above. Then, when I browse the database in documents, it is the same db I copied without the inserts . But the inserts worked because if I can query them from the code. – user1412042 May 23 '12 at 14:41

1 Answers1

0

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 :)

Mutix
  • 4,196
  • 1
  • 27
  • 39