iOS sqlcipher fmdb “File is encrypted or is not a database”
<p><p>这个 <a href="http://www.guilmo.com/fmdb-with-sqlcipher-tutorial/" rel="noreferrer noopener nofollow">tutorial</a>与以下代码配合使用即可。</p>
<pre><code>pod 'FMDB/SQLCipher'
</code></pre>
<p>...</p>
<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = ;
self.databasePath = ;
;
...
}
</code></pre>
<p>...</p>
<pre><code>-(void) createAndCheckDatabase
{
BOOL success;
NSFileManager *fileManager = ;
success = ;
if(success) return; // If file exists, dont do anything
// if file does not exist, make a copy of the one in the Resources folder
NSString *databasePathFromApp = [[ resourcePath] stringByAppendingPathComponent:@"gameDefault.sqlite"]; // File path
; // Make a copy of the file in the Documents folder
// Set the new encrypted database path to be in the Documents Folder
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = ;
NSString *ecDB = ;
// SQL Query. NOTE THAT DATABASE IS THE FULL PATH NOT ONLY THE NAME
const char* sqlQ = [ UTF8String];
sqlite3 *unencrypted_DB;
if (sqlite3_open(, &unencrypted_DB) == SQLITE_OK) {
// Attach empty encrypted database to unencrypted database
sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, NULL);
// export database
sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, NULL);
// Detach encrypted database
sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, NULL);
sqlite3_close(unencrypted_DB);
} else {
sqlite3_close(unencrypted_DB);
NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB));
}
self.databasePath = ;
}
</code></pre>
<p>...</p>
<pre><code>
</code></pre>
<p>...</p>
<pre><code> // FMDatabase
FMDatabase *db = ];
;
;
// FMDatabaseQueue
FMDatabaseQueue *queue = ];
[queue inDatabase:^(FMDatabase *db) {
;
...
}];
</code></pre>
<p>...</p>
<p>当我在其他项目中使用相同的代码时,它可以很好地加密现有的 sqlite 数据库。 <strong>但是当我尝试通过 sql select 查询访问表时,我收到错误消息“文件已加密或不是数据库”。</strong> 不过,教程应用程序没有问题,我可以更新/插入/删除/选择那里的记录。有什么线索吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经解决了。如前所述,相同的代码行为不同,因此设置可能有问题。在我的应用程序中,我添加了 SQLCipher 和 FMDB,如下所示:</p>
<pre><code>pod 'SQLCipher', '~> 3.1'
pod 'FMDB', '~> 2.5'
</code></pre>
<p>在该教程中,它们被添加为</p>
<pre><code>pod 'FMDB/SQLCipher'
</code></pre>
<p>不过,此 pod 命令根据此处显示的终端输出下载相同版本的 SQLCipher 和 FMDB</p>
<pre><code>Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing FMDB (2.5)
Installing SQLCipher (3.1.0)
Generating Pods project
Integrating client project
</code></pre>
<p>所以,我像教程中的那样更改了 pod 命令,并且该错误已修复。不过,我不知道这些命令有何不同。</p></p>
<p style="font-size: 20px;">关于iOS sqlcipher fmdb“File is encrypted or is not a database”,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32039702/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32039702/
</a>
</p>
页:
[1]