3

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

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

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

4

2 回答 2

1

尝试这个

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];

   }
于 2013-01-30T04:05:59.450 回答
1

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

截屏

于 2013-01-30T04:18:01.463 回答