1

我正在使用 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);

}
4

1 回答 1

0

您的代码似乎假设数据库已经存在。您是否尝试打开现有的未加密数据库,然后使用 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

于 2012-01-20T15:46:00.053 回答