2

I am using encryption for my Sq-lite DB in Delphi 10.2 I can encrypt the database and access it setting the password in connection parameters - fine. I can decrypt it and access it when connection parameters password is blank. Fine.

The problem is that I want to use a single program to access the DB whether it is encrypted or not. If the DB is UNEncrypted, but a password is in the connection parameters, I get an error message the database "is not encrypted". Trying to set the password blank or remove it "on the fly", how can I detect the unencrypted state in time to remove the password?

I tried:

procedure TDataApp10.ConnectAppError(ASender, AInitiator: TObject;
  var AException: Exception);
begin
  ConnectApp.Connected:= False;
//handle most likely connect error, DB NOT encryped!
  FDSQLITESecurity1.Database:= 'C:\VCDat\VCDataApp.sdb';
  FDSQLITESecurity1.Password:= 'MyPW';
  FDSQLITESecurity1.RemovePassword; //HANGS MESSAGE HERE!!!
  ConnectApp.Params.Password:='';
  ConnectApp.Connected:= True; //retry or on demand
end;

The hangup message is CIPHER: DB not encrypted

1

1 Answer 1

2

To open an unecrypted database when having Password paramater specified, you can do this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDConnection.Params.Add('DriverID=SQLite');
  FDConnection.Params.Add('Database=C:\MyUnencryptedData.db');
  FDConnection.Params.Add('Password=1234');

  try
    FDConnection.Open;
  except
    { if the engine reports unencrypted database, remove the password from connection
      parameters and retry to open the connection }
    on E: EFDDBEngineException do
      if E.FDCode = er_AD_SQLiteDBUnencrypted then
      begin
        FDConnection.Params.Values['Password'] := '';
        FDConnection.Open;
      end
      else
        raise;
  end;
  ...
end;

Another way is e.g. calling CheckEncryption method and comparing its result (internally happens the similar as above; I just don't like the string return, so I would personally prefer the above):

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDConnection.Params.Add('DriverID=SQLite');
  FDConnection.Params.Add('Database=C:\MyUnencryptedData.db');
  FDConnection.Params.Add('Password=1234');

  FDSQLiteSecurity.Database := 'C:\MyUnencryptedData.db';
  FDSQLiteSecurity.Password := '1234';

  if FDSQLiteSecurity.CheckEncryption = '<unencrypted>' then
    FDConnection.Params.Values['Password'] := '';

  FDConnection.Open;
  ...
end;
Sign up to request clarification or add additional context in comments.

2 Comments

Very good. I did discover the second method about the time this answer was posted. The insight learned is that the FDSQLiteSecurity routines affect the ability to access to the DB, but it is the connection parameters of the DB itself that I needed to focus on to blank the password, etc.
Yes, if you specify non empty Password connection parameter, you're doing that for opening an encrypted database (if not changing an encrypted database password). It's not that misleading because SQLite has no concept of login/password in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.