【发布时间】:2013-11-29 18:51:33
【问题描述】:
我将sqlite 文件转换为NSData 以将其保存在文档目录中。现在我将其转换为sqlite 文件并将该文件保存在文档目录中,但新的sqlite 文件不是有效的sqlite 文件,代码现在不会从中读取数据。下面是我用来将 NSData 转换为 sqlite 文件的代码:
-(void)openDataBase{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Demo.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
NSData *data=[[NSData alloc]initWithContentsOfFile:writableDBPath];
NSString * _key = @"111";
NSData *decryptedData = [data AES256DecryptWithKey:_key];
[fileManager createFileAtPath:writableDBPath contents:decryptedData attributes:nil];
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Demo.sqlite"];
NSData *SqliteData = [NSData dataWithContentsOfFile:defaultDBPath];
NSString * _key = @"111";
NSData *encryptedData = [SqliteData AES256EncryptWithKey:_key];
success= [fileManager createFileAtPath:writableDBPath contents:encryptedData attributes:nil];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);} }
我的代码有什么问题,我想我一直在创建新的 sqlite 文件,但是如何解决这个问题?
任何人都可以帮助我如何将NSData 转换为 sqlite 文件。
我应该在库中编写 sqlite 文件然后从那里访问它吗?
但是每次更新都会解密 sqlite 文件,我必须解密它。
【问题讨论】:
-
赞成以新的方式尝试事物..
标签: ios objective-c sqlite nsdata nsfilemanager