读取encryption的数据库不适用于iOS 10

我正在开发iOS应用程序。 EncryptionDecryption读取和写入工作,直到iOS 9 。 但升级到iOS 10它开始给以下消息“文件被encryption或不是数据库”的问题。

对于DB encryption我使用以下代码:

 sqlite3 *db1; if (sqlite3_open([[self.databaseURL path] UTF8String], &db1) == SQLITE_OK) { const char* key = [@"strong" UTF8String]; sqlite3_key(db1, key, (int)strlen(key)); if (sqlite3_exec(db1, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) { NSLog(@"Password is correct, or a new database has been initialized"); } else { NSLog(@"Incorrect password!"); } sqlite3_close(db1); } 

它工作得很好。

对于打开和阅读操作,我使用以下代码:

 -(void)openDB { NSString *docsDir; docsDir = [self getDirectoryPath]; aPath = [docsDir stringByAppendingPathComponent: @"SQLITE_DEMO.sqlite"]; dbpath = [aPath UTF8String]; } 

读:

 if (sqlite3_open(dbpath, &contactDBNew) == SQLITE_OK) { NSString querySQL = [NSString stringWithFormat:@"SELECT FROM USER"]; const char *query_stmt = [querySQL UTF8String]; char *err; int check = sqlite3_exec(contactDBNew, query_stmt, NULL, NULL, &err); if (sqlite3_prepare_v2(contactDBNew, query_stmt, -1, &statement, NULL) == SQLITE_OK) { // Successfully executed. } else { // Error in execution. } } 

在读取准备语句时出现以下错误消息:“文件已encryption或不是数据库”。

请build议我失踪!

您需要运行PRAGMA key =或在打开每个数据库后使用sqlite3_key。 实际上,最好在应用程序启动时打开一次数据库,最后closures它。 由于密钥派生,反复打开和closures数据库是非常昂贵的。