主要就是設置 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;