阅读加密数据库不适用于iOS 10
我正在开发一个 iOS
应用程序。 加密
& 解密
读取&写入正在工作,直到 iOS 9
。但是,升级到 iOS 10
后,它开始提供以下消息,即文件被加密或不是数据库。
I'm working on an iOS
application. Encryption
& Decryption
to read & write was working till iOS 9
. But after upgrade to iOS 10
it started to giving issue with following message that "file is encrypted or is not a database".
对于 DB加密
我使用以下代码:
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);
}
&它的工作完全正常。
& it's working perfectly fine.
对于开放阅读操作,我使用以下代码:
For opening and reading operation I'm using following code:
-(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.
}
}
在阅读准备声明时,
Here it get failed while reading prepared statement with following error message: "file is encrypted or is not a database".
请指出我错过了什么!
每个数据库打开后,您需要运行PRAGMA key =或使用sqlite3_key。事实上,最好在应用程序开始时打开数据库一次,然后将其关闭。由于关键推导,反复打开和关闭数据库是非常昂贵的。
You need to run PRAGMA key= or use sqlite3_key after every database open. In fact, it is best to open the database once at the start of your application, then close it at the end. It is very expensive to repeatedly open and close the database due to key derivation.