0

我正在使用 xcode 4.5 和 ios 模拟器 6.0,我想删除正在使用的数据库,但我不知道如何操作。我想建立一个全新的数据库。

我读到可以只删除 Macintosh/Library/Application Support/iOS Simulator 下的文件。但是这个文件夹在我的 mac 上不存在。

那么如何删除我的数据库呢?

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

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

    char *error;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    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 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);
        }
    }
}
4

1 回答 1

0

只需在applicationDidFinishLaunching.

if([fileManager fileExistsAtPath:dbPathString]) {
   [fileManager removeItemAtPath:dbPathString error:&error];
}
于 2013-01-16T17:03:34.757 回答