gpt4 book ai didi

苹果手机/iPad : I got "a file is encrypted or is not a database" error while using the pragma key in sqlite cipher?

转载 作者:行者123 更新时间:2023-11-28 17:40:32 26 4
gpt4 key购买 nike

我正在使用 ios5.0、Xcode 4.2 和 sqlite 3。我可以创建数据库和表,也可以在表中读写。

但是如果我使用 sqlcipher,则会收到错误“文件已加密或不是数据库”。请解释一下,为什么我会收到这些错误?。我附上了代码。请找到它...并在此先致谢。

-(void) readFromDatabase {
// Setup the database object
sqlite3 *database;
devicesArray = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from patient;";
sqlite3_stmt *compiledStatement;
// const char* key = [@"test" UTF8String];
// NSLog(@"Length %lu" , strlen(key));
// sqlite3_key(database, key, strlen(key));

sqlite3_exec(database, "PRAGMA KEY='test123';", NULL, NULL, NULL);
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );

int returnCode = sqlite3_exec(database, (const char*) "SELECT count(*) FROM patient;", NULL, NULL, NULL);
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
NSLog(@"%d",returnCode); // the return code is 26 and getting the error


if (sqlite3_exec(database, (const char*) "SELECT count(*) FROM patient;", NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"Success");

} else {
NSLog(@"Failure");

}

returnCode = sqlite3_prepare_v2( database, sqlStatement, -1, &compiledStatement, nil);
printf( "could not prepare statemnt1: %s\n", sqlite3_errmsg(database) );
NSLog(@"%d",returnCode); //the return code is 26 and getting the error


if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];


devices *d = [[devices alloc] initWithName:aName description:aDescription url:aImageUrl];

[devicesArray addObject:d];

[devices release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

}

最佳答案

您的代码似乎假设数据库已经存在。您是否尝试打开一个现有的、未加密的数据库,然后使用 SQLCipher 对其进行加密?如果是这样,你正在做的事情将行不通。

sqlite3_key 函数不加密现有数据库。如果您想加密现有数据库,您需要附加一个新的加密数据库并在两者之间移动数据,如下所述:

http://zetetic.net/blog/2009/12/29/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher/

或者,使用 SQLCipher 2,您可以使用 sqlcipher_export,它提供了一种在数据库之间移动数据的简单方法。

http://groups.google.com/group/sqlcipher/msg/76d5b03426419761

关于苹果手机/iPad : I got "a file is encrypted or is not a database" error while using the pragma key in sqlite cipher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8336552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com