连接到sqlite数据库

时间:2012-11-25 09:56:48

标签: objective-c xcode

嗨,新的Iphone程序员在这里

我正在尝试连接到我的sqlite数据库,但遇到问题,我有sqlite库,并且我的支持文件夹中有数据库文件,它 我加载模拟器时没有连接

 (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *docsDir;
        NSArray *dirPaths;

         //Get the documents directory
         dirPaths =          NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

  docsDir = [dirPaths objectAtIndex:0];

        //Builds the path to the database file
        databasePath = [[NSString alloc]
        initWithString:[docsDir                    stringByAppendingPathComponent:@"PetrolWatch.sqlite"]];

        NSFileManager *fileMgr = [NSFileManager defaultManager];

              if([fileMgr fileExistsAtPath:databasePath]== YES)
              {
               lblStatus.text = @"Path Exists";

              }
              else
              {
              lblStatus.text = @"Failed to open db";
              }

}

提前谢谢

1 个答案:

答案 0 :(得分:1)

您正在文档目录中查找SQLite文件,尽管资源已加载到应用程序包,然后手动复制到文档目录。因为我看不到将文件从包中移出的代码,所以[[NSBundle mainBundle]pathForResource:]可能会更有效地检查文件是否存在(nil path = no resource)。

相关问题