user1882812
user1882812

Reputation: 946

Xcode cannot find path to my database

im searching for some hours now but i cant find the path to my database created with xcode for iOS app. The Database exists, cause its loading the data into my app but i cannot find the path to my database. im using xcode 4.5 and ios-simulator 6.0.

I found some folders under Xcode/contents/ i searched ther for my database but it wasnt successfully ^^.

So where is it stored now?

- (void)createOrOpenDB {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [path objectAtIndex:0];

    dbPathString = [docPath stringByAppendingPathComponent:@"WorkoutPlans.db"];

    char *error;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //[fileManager removeItemAtPath:dbPathString error:nil];

    if(![fileManager fileExistsAtPath:dbPathString]) {
        const char *dbPath = [dbPathString UTF8String];

        if (sqlite3_open(dbPath, &workoutPlansDB) == SQLITE_OK) {
            const char *sql_cr_wp = "CREATE TABLE IF NOT EXISTS WORKOUTPLANS (ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , NAME TEXT NOT NULL  UNIQUE , DESCRIPTION TEXT NOT NULL  UNIQUE)";
            const char *sql_cr_en = "CREATE TABLE IF NOT EXISTS ENTITY (ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , NAME TEXT NOT NULL  UNIQUE , DESCRIPTION TEXT NOT NULL  UNIQUE )";
            const char *sql_cr_ex = "CREATE TABLE IF NOT EXISTS EXERCISE (ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , NAME TEXT NOT NULL  UNIQUE , DESCRIPTION TEXT NOT NULL  UNIQUE , REPS TEXT NOT NULL , WEIGHT TEXT NOT NULL)";
            const char *sql_cr_plen = "CREATE TABLE IF NOT EXISTS WPEN (PLANID, ENID, FOREIGN KEY(PLANID) REFERENCES WORKOUTPLANS(ID) ON DELETE CASCADE, FOREIGN KEY(ENID) REFERENCES ENTITY(ID) ON DELETE CASCADE)";
            const char *sql_cr_enex = "CREATE TABLE IF NOT EXISTS ENEX (ENTITYID, EXERCISEID, FOREIGN KEY(ENTITYID) REFERENCES ENTITY(ID) ON DELETE CASCADE, FOREIGN KEY(EXERCISEID) REFERENCES EXERCISE(ID) ON DELETE CASCADE)";

            sqlite3_exec(workoutPlansDB, sql_cr_wp, NULL, NULL, &error);
            sqlite3_exec(workoutPlansDB, sql_cr_ex, NULL, NULL, &error);
            sqlite3_exec(workoutPlansDB, sql_cr_en, NULL, NULL, &error);
            sqlite3_exec(workoutPlansDB, sql_cr_plen, NULL, NULL, &error);
            sqlite3_exec(workoutPlansDB, sql_cr_enex, NULL, NULL, &error);
            sqlite3_close(workoutPlansDB);
        }
    }
}

Upvotes: 1

Views: 789

Answers (1)

iVenky
iVenky

Reputation: 441

Your database will be stored in the following path

/Users/SystemName/Library/Application\ Support/iPhone\ Simulator/6.1/Applications/Application UniqueName/Documents\databasename

Xcode, in conjunction with the iPhone Simulator, creates a new app folder for each build of your app and will move the the old documents folder and its contents over to the new app folder. It is up to you to check the contents of the documents folder and decide whether a file you have in your Bundle is preferable for use over the one that is in the documents folder. You should make this decision wisely and not lightly.

This is the right behavior. The user of your software may have made some significant changes and modifications to the database. They probably would not like it if, when upgrading for a minor bug fix or even for feature enhancements, their database changes, their high scores, their modified images, or whatever your app saves, suddenly gets replaced with a generic version.

What you should do is to compare the data in your new database, vs the data the user has, and in the case of SQLite, your should import or copy the user's entry into your new database, and then you can move your new database over to the documents folder, removing the user's but replacing it with one that has the user's data and your new data.

In short: make sure you don't replace an existing database with a new one from your bundle, since that will blow away the user's changes.

Upvotes: 2

Related Questions