将单独的.sqlite文件添加到ios项目 [英] add Separate .sqlite file to ios project
问题描述
我已经从SQLite Manager firefox插件创建了myDB.sqlite数据库.我想将此myDB.sqlite文件添加到我的项目中.然后,我可以编写函数以从表中获取数据.
I have created a myDB.sqlite database from SQLite Manager firefox addon. I want to add this myDB.sqlite file to my project. Then I can write functions to get data from tables.
我试图将myDB.sqlite文件添加到项目文件夹&创建一个这样的文件路径.但是我认为该文件路径是错误的.
I tried to add the myDB.sqlite file to project folder & create a filepath like this. But I think this filepath is wrong.
-(NSString *) filePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"document.sqlite"];
return path;
}
将myDB.sqlite文件添加到项目文件夹是否正确?或将myDB.sqlite文件保存在何处&我应该使用什么文件路径?
Does it correct to add myDB.sqlite file to the project folder? or where should I save this myDB.sqlite file & what is the filepath I should use?
推荐答案
将myDB.sqlite文件添加到项目中.右键单击您的项目名称=>将文件添加到"projectName",然后添加它.
add your myDB.sqlite file in your project. by right click your project name=> add files to ""projectName", then add it.
要获取文件路径,可以在appDelegate中添加它
to get your file path you can add this in your appDelegate
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"myDB.sqlite"];
}
完成启动方法调用[self copyDatabaseIfNeeded];
in your did finish with launching method call [self copyDatabaseIfNeeded];
这篇关于将单独的.sqlite文件添加到ios项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!