Search code examples
iosobjective-csqlitensdatafmdb

Get Sqlite database from NSData instead of file


I used to use FMDB to read sqlite data from a file.

FMDatabase *db = [FMDatabase databaseWithPath:filePath];

Now I want to encrypt the file. When the app opens, it will decrypt the file into NSData object.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  NSData *decryptedData = [self getDecryptedData]; 

 return YES;

}

Is there any way I can get FMDatabase object from NSData ? I don't want to save NSData to a file because it is decrypted.


Solution

  • I think FMDatabase and SQLite in general is pretty much bound with file system stuff, that means there's possibly no way to do this by default.

    Your options as I guess:

    • use separate files for encrypted NSData and another one for decrypted SQLite DB – but this solution loses any benefit of having the first file

    • store NSData in a file and work with temporary or in-memory SQLite DB (see https://github.com/ccgus/fmdb#database-creation) which you need to import data in when launching and export from when shutting down

    • more sophisticated method using system-wide Security stuff, but I have really no experience with this