-1

我的项目中有一个名为 mydb.sqlite 的数据库,我使用了:

databasePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];

为了获得路径和 ir 返回databasePath --> null,我检查了我的“复制捆绑资源”,它出现在这里。我想我错过了什么。

4

2 回答 2

0

问题是我在调用 [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"]; 一个 NSObject。这就是它可以找到数据库的原因。现在我在 ControlView 中调用它

于 2013-05-07T11:58:02.437 回答
0
-(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:[self databasePath]];

   NSLog(@"databasePath = %@",databasePath);

   NSLog(@"Success = %d",success);

// 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:databaseName error:nil];

}


-(NSString *)databasePath
{
   NSString *docsDir;
   NSArray *dirPaths;

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

   docsDir = [dirPaths objectAtIndex:0];

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

   return databasePath;
}

在 AppDelegate 的 applicationDidFinishLaunching 中调用这个检查和创建函数

希望它会帮助你。

于 2013-05-07T11:20:05.143 回答