iPhone / ipad :我在sqlite密码中使用编译指示键时遇到"a file is encrypted or is not a database"错误?



我使用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: %sn", sqlite3_errmsg(database) );
        int returnCode = sqlite3_exec(database, (const char*) "SELECT count(*) FROM patient;", NULL, NULL, NULL);
         printf( "could not prepare statemnt: %sn", 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: %sn", 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函数不加密现有数据库。如果要加密现有数据库,则需要ATTACH一个新的加密数据库并在两者之间移动数据,如下所述:

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

相关内容

最新更新