Convert sqlite database to nsdata and vice versa

I converted the file sqliteto NSDatato save it in the document directory. Now I convert it to a file sqliteand save this file in the document directory, but the new sqlitefile is not a valid sqlite file, and the code does not read data now. Below is the code that I use to convert NSDatato sqlite file:

-(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]);} }


What is wrong in my code, I think all the time I create a new sqlite file, but how to solve this problem?
Can someone help me how can I convert NSDatato sqlite file. Should I write a sqlite file in the library and then access it? But every update would decrypt the sqlite file, I have to decrypt it.

+4
2

, . , . , , .

+1

Sqlite , (NSLibraryDirectory) , , (NSFileModificationDate) lib sqlite, , Doc- DB (AES256DecryptWithKey:), .
.
....!!!!!!

0

All Articles