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

梦想的初衷 提交于 2021-01-28 00:58:00

问题


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:


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



来源:https://stackoverflow.com/questions/10715865/documents-folder-is-empty-in-simulator-where-is-my-db-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!