【问题标题】:sqlite connection with Objective C与Objective C的sqlite连接
【发布时间】:2013-01-13 19:47:31
【问题描述】:

我有一段简单的代码用于连接我的 sqliteDb。 我的 sqlite3_prepare_v2 虽然反复失败。我将其缩小到以下代码:

NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"xyz" ofType:@"sqlite3"];

sqLiteDb 返回空值。我不知道为什么-尽我所能并关注了许多线程。 在这里真的很挣扎 - 请帮忙。

【问题讨论】:

    标签: objective-c sqlite


    【解决方案1】:

    您确定您的 xyz.sqlite3 文件包含在正确的 Target Membership 中吗?如果不包含它,则在构建时不会将其复制到您的包中。

    【讨论】:

      【解决方案2】:

      试试这个

      in.h 文件

       NSString *databasePath;
      
      
      in .m file
      
      
      NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,               NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        databasePath = [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];    
        [self checkAndCreateDatabase];
      
      -(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];
      
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 2011-10-25
        • 1970-01-01
        相关资源
        最近更新 更多