Database version is not set to encrypted database file


#1

below is my code. I have one database file.i am creating one encrypted database file for sending through email.Actually my feature is Import/Export database. When i set database version , it always set to 0(zero.).how to set database version to encrypted file.My encryption function working fine.

// Get NSDate for current date
let date = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = “E_dd_MMM_yyyy”
let convertedDate = dateFormatter.string(from: date as Date)

      // generate random number
      //  Make a variable equal to a random number....
      let randomNum:UInt32 = arc4random_uniform(999) // range is 0 to 99
      let randomString:String = String(randomNum)  // convert the UInt32 to some other  types//string works too
      let originalDatabasePath = Util.getPath("MyInfoDatabase.db")
      let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
      _ = FileManager.default
      let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("MyInfo.db")
      _ = fullDestPath?.path
      
      let appendString = randomString + "_" + convertedDate + ".db"
      let newFullDestPath = NSURL(fileURLWithPath: destPath)
      var newDstPath = newFullDestPath.path
      newDstPath = newDstPath! + "/" + "MyInfo" + "_" + appendString
      let dbPath = "MyInfo" + "_" + randomString + "_" + convertedDate + ".db"
      let encryptedFilePath = String(format: "\(newDstPath!)")
      
       // Encryption code
      var rc: Int32
      var stmt: OpaquePointer? = nil
      var db: OpaquePointer? = nil

      if (sqlite3_open(originalDatabasePath, &db) == SQLITE_OK) {
        
        rc = sqlite3_prepare(db, "PRAGMA cipher_version;", -1, &stmt, nil)
        if (rc != SQLITE_OK) {
          let errmsg = String(cString: sqlite3_errmsg(db))
          NSLog("Error preparing SQL: \(errmsg)")
        }
      
        let newQuery = "ATTACH DATABASE '\(encryptedFilePath)' AS encrypted KEY '\(PasswordString!)';"
        
        if sqlite3_exec(db, newQuery, nil, nil, nil) == SQLITE_OK {
          
          if sqlite3_exec(db, "SELECT sqlcipher_export('encrypted');", nil, nil, nil) == SQLITE_OK {
            
            if sqlite3_exec(db, "PRAGMA user_version = 4;", nil, nil, nil) == SQLITE_OK {
              if sqlite3_exec(db, "DETACH DATABASE encrypted;", nil, nil, nil) == SQLITE_OK {
                
               print("Encryption succesful")
                //  Util.invokeAlertMethod("Success", strBody: "You have successfully export database.", delegate: nil)
              } else {
                let errmsg = String(cString: sqlite3_errmsg(db))
                NSLog("DETACH DATABASE encrypted: \(errmsg)")
              }
            }
           } else {
            let errmsg = String(cString: sqlite3_errmsg(db))
            NSLog("Error sqlcipher_export: \(errmsg)")
          }
        } else {
          let errmsg = String(cString: sqlite3_errmsg(db))
          NSLog("Error opening database: \(errmsg)")
        }
        sqlite3_close(db);
        
      } else {
        sqlite3_close(db);
        sqlite3_errmsg(db);
      }

#2

Hi @Ganesh_Padole

Are you trying to set the user version of the encrypted database? If so, you will need to include the alias name for it to apply to that database. In your example above you would need this instead:

PRAGMA encrypted.user_version = 4;

#3

hi,
how to get encrypted database version.


#4

Hi @Ganesh_Padole

You can identify the database library version using the following:

PRAGMA cipher_version;

You can use the following to identify the user version integer value of the database file itself:

PRAGMA user_version;