簡體   English   中英

iPhone / iPad:在sqlite密碼中使用編譯鍵時出現“文件已加密或不是數據庫”錯誤?

[英]Iphone/ipad : I got “a file is encrypted or is not a database” error while using the pragma key in sqlite cipher?

我正在使用ios5.0,Xcode 4.2和sqlite3。我可以創建數據庫和表,也可以在表中讀寫。

但是,如果我使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM