Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Ios objective c中my cocoa mac应用程序的Sqlite教程概述_Ios_Objective C_Macos_Sqlite_Cocoa - Fatal编程技术网

Ios objective c中my cocoa mac应用程序的Sqlite教程概述

Ios objective c中my cocoa mac应用程序的Sqlite教程概述,ios,objective-c,macos,sqlite,cocoa,Ios,Objective C,Macos,Sqlite,Cocoa,我有一个目标c中mac的cocoa应用程序 现在,在我的应用程序中,我想像在ios应用程序中一样实现sqlite数据库 由于我是mac开发新手,我不知道如何在cocoa应用程序中实现这一点。如何使sqlite连接到我的应用程序 我使用了sqlite.h和sqlite.m文件,并使用了下面的代码 -(void)databaseOpen { NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSU

我有一个目标c中mac的cocoa应用程序

现在,在我的应用程序中,我想像在ios应用程序中一样实现sqlite数据库

由于我是mac开发新手,我不知道如何在cocoa应用程序中实现这一点。如何使sqlite连接到我的应用程序

我使用了sqlite.h和sqlite.m文件,并使用了下面的代码

-(void)databaseOpen
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"SchoolApp.sqlite"];
    database = [[Sqlite alloc] init];
    [database open:myDBnew];
}
- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"SchoolApp.sqlite"];


    /*if([[NSFileManager defaultManager] fileExistsAtPath:writableDBPath]){
     [[NSFileManager defaultManager] removeItemAtPath:writableDBPath error:nil];
     }
     */
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SchoolApp.sqlite"];

    ////NSLog(@"Default : %@",defaultDBPath);
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

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

    }
}
但是如何在我的cocoa应用程序中实现这一点呢

请任何人都能指引我


谢谢…

您应该能够为OSX和iOS重新使用现有代码,因为这将依赖于
基础.framework
类,而不是
UIKit
AppKit


注意:您不应该使用
NSAssert
来表示代码中的错误,因为如果定义了
NS\u BLOCK\u断言,这些错误可以在编译时删除;相反,抛出一个
NSException

您是否在iOS中使用CoreData?否。。。我正在使用sqlite数据库并导入sqlite.h和sqlite.m文件。谢谢回复。那么你是说我也可以对OSX使用相同的代码?路径也应该相同?是;我想不出平台特定代码的任何原因。是的,它起作用了。。。非常感谢。一件事是,我需要将路径行更改为NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES);我从这个链接得到了参考资料。现在它的工作,如果我直接在应用程序文件夹创建数据库,但如何在应用程序文件夹中创建一个文件夹,我将附加我的sqlite文件在我的应用程序名的文件夹。谢谢