Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xcode 4.3.2在iphone中使用sqlite数据库_Iphone - Fatal编程技术网

使用xcode 4.3.2在iphone中使用sqlite数据库

使用xcode 4.3.2在iphone中使用sqlite数据库,iphone,Iphone,如何使用xcode 4.3.2在iphone中使用sqlite数据库。。。。 我在查找数据库路径时遇到问题??因为我在我的应用程序的数据库文件夹中复制了FormDB.sqlite…请帮助我,因为我是iphone编程新手 在Appdelegate文件的ApplicationIDFinishLaunching中调用此方法,但返回success=false - (void) copyDatabaseIfNeeded { //Using NSFileManager we can perform

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

在Appdelegate文件的ApplicationIDFinishLaunching中调用此方法,但返回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"];
}

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
试试这个代码 希望这能帮助你。。。。
:-)

过程应该是相同的,将SQLite数据库与应用程序绑定,在第一次启动时将数据库复制到文档或库目录,使用数据库。您尝试了哪些不起作用的内容?您可能忘记了在第一次启动时将数据库复制到文档目录。。。通常是名为createEditableCopyOfDatabaseIfNeeded或类似的dan(自定义)方法。您将在这里找到一个很好的教程:如果成功并返回,请释放filemanager。另外[…]围绕创建databasePathFromApp的调用