Objective c 检查数据库是否存在

Objective c 检查数据库是否存在,objective-c,iphone,xcode,Objective C,Iphone,Xcode,此方法对我而言始终返回true,即使在我从手机卸载应用程序或从Organizer/Applications/中删除应用程序后也是如此 你能给我一些建议吗 这是我的代码: if ([filemgr fileExistsAtPath:self.DBPath]) 1) 使用标准命名约定——保留以大写字母开头的类名和常量名称。2) DBPath的值是多少?/var/../Documents/PhonePager.db3)在执行此代码之前,您是否在其他地方引用数据库?(SQLite将在第一次引用时创建文

此方法对我而言始终返回true,即使在我从手机卸载应用程序或从Organizer/Applications/中删除应用程序后也是如此

你能给我一些建议吗

这是我的代码:

if ([filemgr fileExistsAtPath:self.DBPath])

1) 使用标准命名约定——保留以大写字母开头的类名和常量名称。2)
DBPath
的值是多少?/var/../Documents/PhonePager.db3)在执行此代码之前,您是否在其他地方引用数据库?(SQLite将在第一次引用时创建文件。)很好。我会查一下的,谢谢。
 self.DBName = @"PhonePager.db";

    // Get the documents directory
    NSArray * documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentDir = [documentsPaths objectAtIndex:0];

    // Build the path to the database file
    self.DBPath = [[NSString alloc] initWithString:[documentDir stringByAppendingPathComponent:self.DBName]];

    // Check if DB exists (database path)
    NSFileManager * filemgr = [NSFileManager defaultManager];

    //self.succes = [[NSFileManager defaultManager] fileExistsAtPath:self.DBPath];
    //if (self.succes) //  !!! check that !!!

    // If exists
    if ([filemgr fileExistsAtPath:self.DBPath])
    {
        NSLog(@"Database exists !");
        const char *dbpath = [self.DBPath UTF8String];

        //Check if DB open
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            NSLog(@"Database opened successfully !");

            //Check if the user exists in DB (table "account" field "billing_account").
            NSString * checkUser = @"SELECT billing_account FROM account;";
            const char * stmtCheckUser = [checkUser UTF8String];
            char * errMsg;

            if (sqlite3_exec(contactDB, stmtCheckUser, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"No user in this field");
                //Go to the authentification page
                UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                JSONViewController * jsonvc = [storyboard instantiateViewControllerWithIdentifier:@"JSONViewController"];
                [[self navigationController] initWithRootViewController:jsonvc];
            }

            else
            {
                NSLog(@"The user exists");
                //Go to the messages's list page
                UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                AccueilViewController * avc = [storyboard instantiateViewControllerWithIdentifier:@"AccueilViewController"];
                [[self navigationController] initWithRootViewController:avc];

    //                UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //                JSONViewController * jsonvc = [storyboard instantiateViewControllerWithIdentifier:@"JSONViewController"];
    //                [[self navigationController] initWithRootViewController:jsonvc];

            }

        }

    }

    else

    /** 1.Create DB -> 2.1 Download sql file -> 2.2 ProgressView during dowloading -> 2.3 Create the tables for DB -> 3.Check if DB open  **/
    {
        NSLog(@"No, the DB does not exist");

        //(1).Create DB
        [self createDB];

        //(2).Download the sql file && ProgressView during dowloading && Create the tables for DB
        [self downloadSQL];

        //(3).Check if database open
        @try
        {
            const char * dbpath = [self.DBPath UTF8String];
            sqlite3_open(dbpath, &contactDB);
        }

        @catch (NSException *exception)
        {
            NSLog( @"NSException caught: name %@, reason %@", exception.name, exception.reason );
            return;
        }

        @finally
        {}

        return;
        NSString * databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.DBName];
        [self.fileManager copyItemAtPath:databasePathFromApp toPath:self.DBName error:nil];
    }

    NSString * databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.DBName];
    [self.fileManager copyItemAtPath:databasePathFromApp toPath:self.DBName error:nil];

}