我的项目中有一个名为mydb.sqlite的数据库,我用过:
databasePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
获取路径并且ir返回databasePath --> null
我检查了我的“Copy Bundle Resources”,它出现在这里。我想我错过了什么。
答案 0 :(得分: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中创建函数
希望它会对你有所帮助。
答案 1 :(得分:0)
问题是我在打电话
[[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
在NSObject中。这就是它可以找到数据库的原因。现在我在ControlView中调用它