检查数据库是否存在

时间:2014-09-02 16:20:09

标签: objective-c iphone xcode

if ([filemgr fileExistsAtPath:self.DBPath])

即使我从手机上卸下应用程序或将其从管理器/应用程序/ ...中删除,此方法也始终为我返回。

你可以给我一些建议吗?

这是我的代码:

 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];

}

0 个答案:

没有答案