0

如何使用 xcode 4.3.2 在 iphone 中使用 sqlite 数据库...。我在查找数据库路径时遇到问题?因为我已经在我的应用程序的数据库文件夹中复制了 FormDB.sqlite ......请帮助我,因为我是 iphone 编程新手......

在 Appdelegate 文件的 applicationDidFinishLaunching 中调用此方法,但它返回 success=false

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"FormDB.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

- (NSString *) getDBPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"FormDB.sqlite"];
}
4

1 回答 1

2

AppDelegate.h文件中声明

@interface AppDelegate{

        NSString *databaseName;
    NSString *databasePath;

}
-(void) checkAndCreateDatabase;
@end

然后在

@implementation AppDelegate{

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        // Setup some globals
        databaseName = @"FormDB.sqlite";

        // Get the path to the documents directory and append the databaseName
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

        // Execute the "checkAndCreateDatabase" function
        [self checkAndCreateDatabase];

        // Query the database for all animal records and construct the "animals" array
        [self readAnimalsFromDatabase];

        // Configure and show the window
        [window addSubview:[navigationController view]];
        [window makeKeyAndVisible];
    }
    -(void) checkAndCreateDatabase{
        // Check if the SQL database has already been saved to the users phone, if not then copy it over
        BOOL success;

        // Create a FileManager object, we will use this to check the status
        // of the database and to copy it over if required
        NSFileManager *fileManager = [NSFileManager defaultManager];

        // Check if the database has already been created in the users filesystem
        success = [fileManager fileExistsAtPath:databasePath];

        // If the database already exists then return without doing anything
        if(success) return;

        // If not then proceed to copy the database from the application to the users filesystem

        // Get the path to the database in the application package
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

        [fileManager release];
    }
}
@end

试试这个代码可以帮助你.... :-)

于 2012-04-21T13:31:44.357 回答