主要就是設置 TFDConnection 的兩個鏈接參數: Password, NewPassword, 非常簡單.
const dbPath = 'C:\Temp\SQLiteTest.sdb'; {建立加密數據庫, 密碼是 mm123} procedure TForm1.FormCreate(Sender: TObject); const strTable = 'CREATE TABLE MyTable(Id integer, Name string(10), Age byte)'; //Id, Name, Age 三個字段 begin if FileExists(dbPath) then DeleteFile(dbPath); FDConnection1.Params.Add('DriverID=SQLite'); FDConnection1.Params.Add('Database=' + dbPath); FDConnection1.Params.Add('Password=mm123'); //相同與 Password=aes-256:mm123; aes-256 是默認的加密算法; //還有: aes-128,aes-192,aes-256, aes-ctr-128,aes-ctr-192,aes-ctr-256, aes-ecb-128,aes-ecb-192,aes-ecb-256 //建表並輸入測試數據 FDConnection1.ExecSQL(strTable); FDConnection1.ExecSQL('INSERT INTO MyTable(Id, Name, Age) VALUES(:1, :2, :3)', [1, 'abc', 23]); end; {打開有密碼的數據庫} procedure TForm1.Button1Click(Sender: TObject); begin FDConnection1.Params.Clear; FDConnection1.Connected := False; FDConnection1.Params.Add('DriverID=SQLite'); FDConnection1.Params.Add('Database=' + dbPath); FDConnection1.Params.Add('Password=mm123'); FDConnection1.Connected := True; FDQuery1.Open('SELECT * FROM MyTable'); end; {修改密碼} procedure TForm1.Button2Click(Sender: TObject); begin FDConnection1.Params.Clear; FDConnection1.Connected := False; FDConnection1.Params.Add('DriverID=SQLite'); FDConnection1.Params.Add('Database=' + dbPath); FDConnection1.Params.Add('Password=mm123'); FDConnection1.Params.Add('NewPassword=mm12345'); //新密碼, 密碼為空表示取消密碼 FDConnection1.Connected := True; FDConnection1.Connected := False; end;
FDSQLiteSecurity1.DriverLink := FDPhysSQLiteDriverLink1; //TFDSQLiteSecurity 和 FireDAC.Phys.SQLite 裡的其他四個控件, 使用前都要先指定這個屬性 FDSQLiteSecurity1.Database := dbPath; //指定數據庫路徑 FDSQLiteSecurity1.Password := 'mm111'; //密碼 FDSQLiteSecurity1.ToPassword := 'mm222'; //新密碼 FDSQLiteSecurity1.SetPassword; //設置密碼 FDSQLiteSecurity1.ChangePassword; //修改密碼 FDSQLiteSecurity1.RemovePassword; //移除密碼