项目目录下的SQLite文件,使用fmdb打开失败
SQLite file in project catalogue, use fmdb open it failed
我在SQLite Manager
中创建了一个table并将其导出到桌面,它的名字是db_fo_dic.sqlite
,我可以打开db_fo_dic.sqlite
使用SQLite Manager
, 所以我将 db_fo_dic.sqlite
复制到我的 Xcode 项目目录中,我尝试通过 FMDB
打开 sqlite 文件,但是失败了:
NSString *path = [[NSBundle mainBundle] pathForResource:@"db_fo_dic" ofType:@"sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
结果信息是:
2016-08-01 19:22:34.955 test_sqlite[15380:425242] The FMDatabase <FMDatabase: 0x7ff589c38f10> is not open.
这是我的sqlite的位置路径:
/Users/youpude/Library/Developer/CoreSimulator/Devices/87D0245A-47F7-4741-81B2-209625CB3C5E/data/Containers/Bundle/Application/47B3AD1F-C08E-4E34-8858-494A95FB9EA5/test_sqlite.app/db_fo_dic.sqlite
您没有对应用程序包中存储的内容的写入权限。所以你必须先将你的数据库复制到文档目录。有关 iOS 中文件系统权限的更多详细信息,请参阅 Apple documentation for file system。
所以你可以这样做,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
self.databaseFilename = dbFilename;
NSString* destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
if (![[NSFileManager defaultManager]fileExistsAtPath:destinationPath]) {
NSString *sourcePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:self.databaseFilename];
NSError *error;
[[NSFileManager defaultManager]copyItemAtPath:sourcePath toPath:destinationPath error:&error];
if (error != nil) {
NSLog(@"%@",[error localizedDescription]);
}
}
此处 dbFilename
是您的数据库名称(在您的例子中:db_fo_dic.sqlite
)。
执行此操作后,尝试从目标路径打开数据库,我认为它会起作用!
8个小时过去了,我终于找到了解决问题的方法。
#import "FMDatabaseQueue.h"
static FMDatabaseQueue *_queue;
在- (void)viewDidLoad
中:
_queue = [FMDatabaseQueue databaseQueueWithPath:path];
我可以查询我的数据库:
[_queue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:@"select * from dic_content limit 10;"];
while ([result next]) {
NSString *str = [result stringForColumn:@"tit"];
NSLog(@"%@",str);
}
}];
我用这个方法代替了之前的方法
我在SQLite Manager
中创建了一个table并将其导出到桌面,它的名字是db_fo_dic.sqlite
,我可以打开db_fo_dic.sqlite
使用SQLite Manager
, 所以我将 db_fo_dic.sqlite
复制到我的 Xcode 项目目录中,我尝试通过 FMDB
打开 sqlite 文件,但是失败了:
NSString *path = [[NSBundle mainBundle] pathForResource:@"db_fo_dic" ofType:@"sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
结果信息是:
2016-08-01 19:22:34.955 test_sqlite[15380:425242] The FMDatabase <FMDatabase: 0x7ff589c38f10> is not open.
这是我的sqlite的位置路径:
/Users/youpude/Library/Developer/CoreSimulator/Devices/87D0245A-47F7-4741-81B2-209625CB3C5E/data/Containers/Bundle/Application/47B3AD1F-C08E-4E34-8858-494A95FB9EA5/test_sqlite.app/db_fo_dic.sqlite
您没有对应用程序包中存储的内容的写入权限。所以你必须先将你的数据库复制到文档目录。有关 iOS 中文件系统权限的更多详细信息,请参阅 Apple documentation for file system。
所以你可以这样做,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = [paths objectAtIndex:0];
self.databaseFilename = dbFilename;
NSString* destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
if (![[NSFileManager defaultManager]fileExistsAtPath:destinationPath]) {
NSString *sourcePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:self.databaseFilename];
NSError *error;
[[NSFileManager defaultManager]copyItemAtPath:sourcePath toPath:destinationPath error:&error];
if (error != nil) {
NSLog(@"%@",[error localizedDescription]);
}
}
此处 dbFilename
是您的数据库名称(在您的例子中:db_fo_dic.sqlite
)。
执行此操作后,尝试从目标路径打开数据库,我认为它会起作用!
8个小时过去了,我终于找到了解决问题的方法。
#import "FMDatabaseQueue.h"
static FMDatabaseQueue *_queue;
在- (void)viewDidLoad
中:
_queue = [FMDatabaseQueue databaseQueueWithPath:path];
我可以查询我的数据库:
[_queue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:@"select * from dic_content limit 10;"];
while ([result next]) {
NSString *str = [result stringForColumn:@"tit"];
NSLog(@"%@",str);
}
}];
我用这个方法代替了之前的方法